+ All Categories
Home > Documents > Graph Introduction

Graph Introduction

Date post: 07-Apr-2018
Category:
Upload: -sufi
View: 227 times
Download: 0 times
Share this document with a friend

of 53

Transcript
  • 8/6/2019 Graph Introduction

    1/53

    CSC2105: AlgorithmsCSC2105: AlgorithmsGraphGraph -- IntroductionIntroduction

    Mashiour RahmanMashiour [email protected]@aiub.edu

    American International University BangladeshAmerican International University Bangladesh

  • 8/6/2019 Graph Introduction

    2/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 2

    GraphGraph -- IntroductionIntroductionReference:Reference:

    CLRS Appendix B.4, Chapters 22, 24-26

    Objectives:Objectives:

    To learn several significant graph algorithms and their

    applications

    a. Graph search (BFS, DFS)

    b. Bellman/Ford and Dijkstras shortest path algorithms (Greedy)

    c. Floyds algorithm (Dynamic Programming)d. Network flows

  • 8/6/2019 Graph Introduction

    3/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 3

    MotivationMotivation

    StateState--space search in Artificial Intelligencespace search in Artificial Intelligence

    Geographical information systems, electronicGeographical information systems, electronic

    street directorystreet directory

    Logistics and supply chain managementLogistics and supply chain management

    Telecommunications network designTelecommunications network design

    Many more industry applicationsMany more industry applications

  • 8/6/2019 Graph Introduction

    4/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 4

    Review: GraphsReview: GraphsGraphGraph GG = (= (VV,, EE))

    V = {1,n} = set of vertices, E = set ofe edges

    Undirectedgraph:

    edge (u, v) = edge (v, u)

    no self-loops

    Directedgraph (digraph):

    edge (u, v) goes from vertex u to vertex v

    Sparse graph:

    e = O(n), dense otherwise

    A weighted graph associates weights with either the edges or thevertices

    Degree of a vertex v:

    deg(v) = number of edges adjacent on v (in-degree and out-degree fordirected graphs)

  • 8/6/2019 Graph Introduction

    5/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 5

    Examples of GraphsExamples of Graphs

    Directed & Undirected graphs.Directed & Undirected graphs.

    a)a) A directed graphA directed graph G= (V, E)G= (V, E), where, whereVV = {1,2,3,4,5,6} and= {1,2,3,4,5,6} andEE={(1,2),(2,2),(2,4),(2,5),(4,1),(4,5),(5,4),(3,6)}. The edge (2,2) is self={(1,2),(2,2),(2,4),(2,5),(4,1),(4,5),(5,4),(3,6)}. The edge (2,2) is self

    loop.loop.b)b) An undirected graphAn undirected graph G = (V,E)G = (V,E), where, whereVV = {1,2,3,4,5,6} and= {1,2,3,4,5,6} and EE ==

    {(1,2),(1,5),(2,5),(3,6)}. The vertex 4 is isolated.{(1,2),(1,5),(2,5),(3,6)}. The vertex 4 is isolated.

    c)c) The subgraph of the graph in part (a) induced by vertex set {1,2,3,6}The subgraph of the graph in part (a) induced by vertex set {1,2,3,6}

  • 8/6/2019 Graph Introduction

    6/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 6

    Review: GraphsReview: Graphs

    Acyclic

    Acyclic: if a graph contains no cycles: if a graph contains no cycles

    DAGDAG: Directed acyclic graphs: Directed acyclic graphs

    ConnectedConnected: if every vertex of a graph can: if every vertex of a graph can reachreach

    every other vertex, i.e., every pair of vertices isevery other vertex, i.e., every pair of vertices isconnected by a pathconnected by a path

    Connected ComponentsConnected Components : equivalence classes of: equivalence classes of

    vertices under is reachable from relationvertices under is reachable from relation

    Strongly connectedStrongly connected: every 2 vertices are reachable: every 2 vertices are reachable

    from each other (in a digraph)from each other (in a digraph)

  • 8/6/2019 Graph Introduction

    7/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 7

    Forests, DAG, ComponentsForests, DAG, Components

  • 8/6/2019 Graph Introduction

    8/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 8

    Review:Representing GraphsReview:Representing GraphsAdjacency matrixAdjacency matrix: represents a graph as: represents a graph as nn xx nn matrixmatrix AA::

    A[i,j] = 1 if edge (i,j) E (or weight of edge)

    = 0 if edge (i,j) E

    Storage requirements: O(n2)

    A dense representationBut, can be very efficient for small graphs

    Especially if store just one bit/edge

    Undirected graph: only need half of matrix

    Adjacency list

    Adjacency list: list of adjacent vertices: list of adjacent vertices

    For each vertex v V, store a list of vertices adjacent to v

    Storage requirements: O(n+e)

    Good for large, sparse graphs (e.g., planar maps)

  • 8/6/2019 Graph Introduction

    9/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 9

    Review:Representing GraphsReview:Representing Graphs

    Two representations of an undirected graph.

    a) An undirected graph G having five vertices and sevenedges.

    b) An adjacency-list representation ofG.

    c) The adjacency-matrix representation ofG.

  • 8/6/2019 Graph Introduction

    10/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 10

    Graph SearchingGraph SearchingGivenGiven: a graph: a graph GG ==(V,E)(V,E), directed or undirected, directed or undirected

    GoalGoal: methodically explore every vertex and edge: methodically explore every vertex and edge

    UltimatelyUltimately: build a: build a treetree on the graphon the graph

    Pick a vertex as the root

    Choose certain edges to produce a tree

    Note: might also build a forestif graph is not connected

    BreadthBreadth--firstsearchfirstsearch

    DepthDepth--firstsearchfirstsearchOther variants: bestOther variants: best--first, iterated deepening search,first, iterated deepening search,etc.etc.

  • 8/6/2019 Graph Introduction

    11/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 11

    DepthDepth--First Search (DFS)First Search (DFS)Explore deeper in the graph whenever possibleExplore deeper in the graph whenever possible

    Edges are exploredout of the mostrecentlydiscoveredvertex vthat

    still has unexplored edges (LIFO)

    When all ofvs edges have been explored, backtrack to the vertex from

    which vwas discovered

    computes 2 timestamps: d[ ] (discovered) and f[ ] (finished)

    builds one or more depth-firsttree(s) (depth-firstforest)

    Algorithm colors each vertexAlgorithm colors each vertex

    WHITE: undiscovered

    GRAY: discovered,inprocess

    BLACK: finished,alladjacentverticeshavebeendiscovered

  • 8/6/2019 Graph Introduction

    12/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 12

    DepthDepth--First Search: The CodeFirst Search: The CodeDFS(G)DFS(G)

    {{

    foreachvertex u V

    color[u] = WHITE;

    time = 0;

    foreachvertex u V

    if(color[u] == WHITE)

    DFS_Visit(u);

    }}

    DFS_Visit(u)DFS_Visit(u)

    {{color[u] = GREY;color[u] = GREY;time = time+1;time = time+1;d[u] = time;d[u] = time; // compute d[]// compute d[]for each v adjacent to ufor each v adjacent to u

    if (color[v] == WHITE)if (color[v] == WHITE)p[v]= up[v]= u // build tree// build treeDFS_Visit(v);DFS_Visit(v);

    color[u] = BLACK;color[u] = BLACK;time = time+1;time = time+1;f[u] = time;f[u] = time; // compute f[]// compute f[]

    }}

  • 8/6/2019 Graph Introduction

    13/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 13

    DFS AnalysisDFS Analysis

    Running time ofRunning time ofDFSDFS=

    =

    O(n+e)O(n+e)DFS (excluding DFS_Visit) takes O(DFS (excluding DFS_Visit) takes O(nn) time) time

    DFS_Visit:DFS_Visit:

    DFS_Visit( v) is called exactly once for each vertex v

    During DFS_Visit( v), adjacency list ofv is scanned once

    sum of lengths of adjacency lists =O(e)

    This type of aggregate analysis is an informalThis type of aggregate analysis is an informalexample ofexample ofamortizedanalysisamortizedanalysis

  • 8/6/2019 Graph Introduction

    14/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 14

    DFS Classification of EdgesDFS Classification of Edges

    DFS can be used to classify edges ofDFS can be used to classify edges ofGG::1. Tree edges: edges in the depth-first forest.

    2. Back edges: edges (u,v) connecting a vertex u to an

    ancestorv in a depth-first tree.

    3. Forward edges: non-tree edges (u,v) connecting a vertex

    u to a descendant v in a depth-first tree.

    4. Cross edges: all other edges.

    DFS yields valuable information about theDFS yields valuable information about the structurestructureof a graph.of a graph.

  • 8/6/2019 Graph Introduction

    15/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 15

    Operations of DFSOperations of DFS

    x zy

    wvu

    Undiscovered

    Discovered,

    On Process

    FinishedFinished, allall

    adjacent verticesadjacent vertices

    have beenhave been

    discovereddiscovered

    Discover time/ Finish time

  • 8/6/2019 Graph Introduction

    16/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 16

    Operations of DFSOperations of DFS

    x zy

    wvu

    x zy

    wv

    1/

    u

    Undiscovered

    Discovered,

    On Process

    FinishedFinished, allall

    adjacent verticesadjacent vertices

    have beenhave been

    discovereddiscovered

    Discover time/ Finish time

  • 8/6/2019 Graph Introduction

    17/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 17

    Operations of DFSOperations of DFS

    x zy

    wvu

    x zy

    wv

    1/

    u

    x zy

    w

    2/

    v

    1/

    u

    Tree edge

    Undiscovered

    Discovered,

    On Process

    FinishedFinished, allall

    adjacent verticesadjacent vertices

    have beenhave been

    discovereddiscovered

    Discover time/ Finish time

  • 8/6/2019 Graph Introduction

    18/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 18

    Operations of DFSOperations of DFS

    x zy

    wvu

    x zy

    wv

    1/

    u

    x zy

    w

    2/

    v

    1/

    u

    Tree edge

    x z

    3/

    y

    w

    2/

    v

    1/

    u

    Undiscovered

    Discovered,

    On Process

    FinishedFinished, allall

    adjacent verticesadjacent vertices

    have beenhave been

    discovereddiscovered

    Discover time/ Finish time

  • 8/6/2019 Graph Introduction

    19/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 19

    Operations of DFSOperations of DFS

    x zy

    wvu

    x zy

    wv

    1/

    u

    x zy

    w

    2/

    v

    1/

    u

    Tree edge

    x z

    3/

    y

    w

    2/

    v

    1/

    u

    4/

    x z

    3/

    y

    w

    2/

    v

    1/

    u

    Undiscovered

    Discovered,

    On Process

    FinishedFinished, allall

    adjacent verticesadjacent vertices

    have beenhave been

    discovereddiscovered

    Discover time/ Finish time

  • 8/6/2019 Graph Introduction

    20/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 20

    Operations of DFSOperations of DFS

    x zy

    wvu

    x zy

    wv

    1/

    u

    x zy

    w

    2/

    v

    1/

    u

    Tree edge

    x z

    3/

    y

    w

    2/

    v

    1/

    u

    4/

    x z

    3/

    y

    w

    2/

    v

    1/

    u

    4/

    x z

    3/

    y

    w

    2/

    v

    1/

    u

    Back Edge

    Undiscovered

    Discovered,

    On Process

    FinishedFinished, allall

    adjacent verticesadjacent vertices

    have beenhave been

    discovereddiscovered

    Discover time/ Finish time

  • 8/6/2019 Graph Introduction

    21/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 21

    Operations of DFSOperations of DFS

    x zy

    wvu

    x zy

    wv

    1/

    u

    x zy

    w

    2/

    v

    1/

    u

    Tree edge

    x z

    3/

    y

    w

    2/

    v

    1/

    u

    4/

    x z

    3/

    y

    w

    2/

    v

    1/

    u

    4/

    x z

    3/

    y

    w

    2/

    v

    1/

    u

    Back Edge

    4/54/5

    x z

    3/

    y

    w

    2/

    v

    1/

    u

    Undiscovered

    Discovered,

    On Process

    FinishedFinished, allall

    adjacent verticesadjacent vertices

    have beenhave been

    discovereddiscovered

    Discover time/ Finish time

  • 8/6/2019 Graph Introduction

    22/53

  • 8/6/2019 Graph Introduction

    23/53

  • 8/6/2019 Graph Introduction

    24/53

  • 8/6/2019 Graph Introduction

    25/53

  • 8/6/2019 Graph Introduction

    26/53

  • 8/6/2019 Graph Introduction

    27/53

  • 8/6/2019 Graph Introduction

    28/53

  • 8/6/2019 Graph Introduction

    29/53

  • 8/6/2019 Graph Introduction

    30/53

  • 8/6/2019 Graph Introduction

    31/53

  • 8/6/2019 Graph Introduction

    32/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 32

    Cycle DetectionCycle DetectionTheorem: An undirected graph isTheorem: An undirected graph is acyclicacycliciff a DFSiff a DFSyields noyields no backedgesbackedges

    ProofProof

    If acyclic, no back edges by definition (because a back edgeimplies a cycle)

    If no back edges, acyclic

    No back edges implies only tree edges (Why?)

    Only tree edges implies we have a tree or a forest

    Which by definition is acyclic

    Thus, can run DFS to find whether a graph has aThus, can run DFS to find whether a graph has acycle.cycle. HowwouldyoumodifythecodetodetectHowwouldyoumodifythecodetodetectcycles?cycles?

  • 8/6/2019 Graph Introduction

    33/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 33

    Directed Acyclic Graph (DAG)Directed Acyclic Graph (DAG)

    Arises in many applications where there areArises in many applications where there areprecedence or ordering constraints (e.g. schedulingprecedence or ordering constraints (e.g. scheduling

    problems)problems)

    if there are a series of tasks to be performed, and certain

    tasks must precede other tasks

    In general, a precedence constraint graph is a DAG,In general, a precedence constraint graph is a DAG,

    in which vertices are tasks and edge (u, v) means thatin which vertices are tasks and edge (u, v) means that

    task u must be completed before task v beginstask u must be completed before task v begins

  • 8/6/2019 Graph Introduction

    34/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 34

    Topological SortTopological SortFind a linear ordering of all vertices of the DAG suchFind a linear ordering of all vertices of the DAG suchthat if G contains an edge (u, v), u appears before vthat if G contains an edge (u, v), u appears before vin the ordering.in the ordering.

    In general, there may be many legal topologicalIn general, there may be many legal topologicalorders for a given DAG.orders for a given DAG.

    IdeaIdea::

    1. CallDFS(G) tocomputefinishingtimef[ ]

    2. Insertverticesontoalinkedlistaccordingtodecreasingorderoff[ ]

    HowtomodifyDFStoperform TopologicalSortin O(n+e)HowtomodifyDFStoperform TopologicalSortin O(n+e)time?time?

  • 8/6/2019 Graph Introduction

    35/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 35

    Strongly Connected ComponentsStrongly Connected Components

    DigraphsDigraphs are often used to model communication andare often used to model communication and

    transportation networkstransportation networks

    People want to know that the networks are complete in thePeople want to know that the networks are complete in the

    sense that from any location it is possible to reach anothersense that from any location it is possible to reach anotherlocation in the digraphlocation in the digraph

    How to find strongly connected components (SCC) of aHow to find strongly connected components (SCC) of a

    digraph?digraph?

  • 8/6/2019 Graph Introduction

    36/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 36

    AlgorithmAlgorithm (SCC)(SCC)

    StronglyStrongly--ConnectedConnected--Components(G)Components(G)1. callDFS(G) tocomputefinishtimesf[]

    2. computeGT

    3. callDFS(GT) andconsiderverticesindecreasingf[]

    computedinstep1

    4. outputverticesofeachtreeinDFS(GT) asseparate

    stronglyconnectedcomponent

    Total running timeTotal running time T(n+e)T(n+e)

  • 8/6/2019 Graph Introduction

    37/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 37

    BreadthBreadth--First Search (BFS)First Search (BFS)Given source vertexGiven source vertex ss,,

    systematically explore the breadth of the frontier to

    discover every vertex reachable from s

    computes the distance d[ ] from s to all reachable vertices

    builds a breadth-firsttree rooted at s

    AlgorithmAlgorithm

    colors each vertex:

    WHITE : undiscovered

    GRAY: discovered, in process

    BLACK: finished, all adjacent vertices have been discovered

  • 8/6/2019 Graph Introduction

    38/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 38

    BFS (Intuition)BFS (Intuition)

  • 8/6/2019 Graph Introduction

    39/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 39

    BFS: The CodeBFS: The CodeBFS(G,s) {BFS(G,s) {

    initializevertices;initializevertices;Q = {s};Q = {s};

    while(Qnotempty) {while(Qnotempty) {

    u = Dequeue(Q);u = Dequeue(Q);

    foreachvadjacenttoudo {foreachvadjacenttoudo {

    if(color[v] == WHITE) {if(color[v] == WHITE) {

    color[v] = GRAY;color[v] = GRAY;

    d[v] = d[u] +1;//computed[]d[v] = d[u] +1;//computed[]

    p[v] = u;//buildBFStreep[v] = u;//buildBFStree

    Enqueue(Q,v);Enqueue(Q,v);

    }}

    }}

    color[u] = BLACK;color[u] = BLACK;

    }}

    }}

  • 8/6/2019 Graph Introduction

    40/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 40

    BFS ExampleBFS Example

  • 8/6/2019 Graph Introduction

    41/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 41

    BFS ExampleBFS Example

  • 8/6/2019 Graph Introduction

    42/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 42

    BFS ExampleBFS Example

  • 8/6/2019 Graph Introduction

    43/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 43

    BFS ExampleBFS Example

  • 8/6/2019 Graph Introduction

    44/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 44

    BFS ExampleBFS Example

  • 8/6/2019 Graph Introduction

    45/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 45

    BFS ExampleBFS Example

  • 8/6/2019 Graph Introduction

    46/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 46

    BFS ExampleBFS Example

  • 8/6/2019 Graph Introduction

    47/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 47

    BFS ExampleBFS Example

  • 8/6/2019 Graph Introduction

    48/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 48

    BFS ExampleBFS Example

  • 8/6/2019 Graph Introduction

    49/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 49

    BFS ExampleBFS Example

  • 8/6/2019 Graph Introduction

    50/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 50

    BFS ExampleBFS Example

  • 8/6/2019 Graph Introduction

    51/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 51

    BFS ExampleBFS Example

  • 8/6/2019 Graph Introduction

    52/53

    Mashiour Rahman AIUB::CSC2105::Algorithms Graph Introduction 52

    BFS AnalysisBFS Analysisinitializeinitialize :: O(n)O(n)

    LoopLoop: Queue operations and Adjacency checks: Queue operations and Adjacency checks

    Queue operationsQueue operations

    each vertex is enqueued/dequeued at most once. Why?

    each operation takes O(1) time, hence O(n)

    Adjacency checksAdjacency checks

    adjacency list of each vertex is scanned at most oncesum of lengths of adjacency lists =O(e)

    Total run time of BFS =Total run time of BFS =O(n+e)O(n+e)

  • 8/6/2019 Graph Introduction

    53/53

    BreadthBreadth--First Search: PropertiesFirst Search: Properties

    What do we get the end of BFS?What do we get the end of BFS?

    1.1. d[v]d[v] ==shortestshortest--pathdistancepathdistance fromfrom ss toto vv, i.e., i.e.

    minimum number of edges fromminimum number of edges from ss toto vv, or, or ififvvnotnot

    reachable fromreachable from ss

    Proof : referCLRS

    2.2. aa breadthbreadth--firstfirsttreetree, in which path from root, in which path from root ss toto

    any vertexany vertex vv represent a shortest pathrepresent a shortest path

    Thus can use BFS to calculate shortest path from onevertex to another in O(n+e) time, for unweighted graphs.


Recommended