Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337.

Similar presentations


Presentation on theme: "Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337."— Presentation transcript:

1 Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

2 Graphs2 Graph A graph is a pair (V, E), where –V is a set of nodes, called vertices –E is a collection (can be duplicated) of pairs of vertices, called edges –Vertices and edges are data structures and store elements Example: –A vertex represents an airport and stores the three-letter airport code –An edge represents a flight route between two airports and stores the mileage of the route ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142

3 Graphs3 Edge Types Directed edge –ordered pair of vertices (u,v) –first vertex u is the source or origin –second vertex v is the destination –e.g., a flight Undirected edge –unordered pair of vertices (u,v) –e.g., a flight route Directed graph (digraph) –all the edges are directed –e.g., flight network Undirected graph –all the edges are undirected –e.g., route network ORDPVD flight AA 1206 ORDPVD 849 miles

4 Graphs4 Applications Electronic circuits –Printed circuit board –Integrated circuit Transportation networks –Highway network –Flight network Computer networks –Local area network –Internet –Web Databases –Entity-relationship diagram

5 Graphs5 Terminology End vertices (or endpoints) of an edge –U and V are the endpoints of a Edges incident on a vertex –a, d, and b are incident on V Adjacent vertices – share edge –U and V are adjacent Degree of a vertex –X has degree 5 Parallel edges (go between same nodes) –h and i are parallel edges Self-loop (same nodes is origin/destination) –j is a self-loop XU V W Z Y a c b e d f g h i j

6 Graphs6 P1P1 Terminology (cont.) Simple graphs have no parallel (multiple edges between same vertices) or self loop Path –sequence of alternating vertices and edges which match up Cycle - path with same first and last vertex Simple path –path such that all its vertices and edges are distinct Examples –P 1 =(V,b,X,h,Z) is a simple path –P 2 =(U,c,W,e,X,g,Y,f,W,d,V) is a path that is not simple XU V W Z Y a c b e d f g hP2P2

7 Graphs7 Terminology (cont.) Simple cycle –cycle such that all its vertices and edges are distinct Connected graph/component (Spanning – contains all vertices) subgraph Forest (acyclic), free (no root) trees (connected forest), spanning tree Examples –C 1 =(V,b,X,g,Y,f,W,c,U,a,  ) is a simple cycle –C 2 =(U,c,W,e,X,g,Y,f,W,d,V,a,  ) is a cycle that is not simple C1C1 XU V W Z Y a c b e d f g hC2C2

8 Graphs8 Properties Notation nnumber of vertices mnumber of edges deg(v)degree of vertex v Property 1  v deg(v)  2m Proof: each edge is counted twice Property 2 In an undirected graph with no self-loops and no multiple edges m  n (n  1)  2 Proof: each vertex has degree at most (n  1) What is the bound for a directed graph? Example n  4 m  6 deg(v)  3

9 Graphs9 Main Methods of the Graph ADT Vertices and edges –are positions –store elements Accessor methods –aVertex() –incidentEdges(v) –endVertices(e) –isDirected(e) –origin(e) –destination(e) –opposite(v, e) –areAdjacent(v, w) Update methods –insertVertex(o) –insertEdge(v, w, o) –insertDirectedEdge(v, w, o) –removeVertex(v) –removeEdge(e) Generic methods –numVertices() –numEdges() –vertices() –edges()

10 Edge List Structure We could just store a list of nodes and a list of edges. Vertex info: name, id, degree, … Edge info: directed?, endpoints, weight It wouldn’t be all that convenient for doing a traversal, but if our operations involved looking at each edge or each node, it would be simple. Note: edges point to vertexes (not visa versa) 10

11 Graphs11 Edge List Structure Vertex object –element –reference to position in vertex sequence Edge object –element –origin vertex object –destination vertex object –reference to position in edge sequence Vertex sequence –sequence of vertex objects Edge sequence –sequence of edge objects v u w ac b a z d uv w z bcd

12 Adjacency List Structure Edge list structure Incidence sequence for each vertex –sequence of references to edge objects of incident edges Augmented edge objects –references to end vertices 12

13 Graphs13 Adjacency Matrix Structure Edge list structure Augmented vertex objects –Integer key (index) associated with vertex 2D adjacency array –Reference to edge object for adjacent vertices –Null for non nonadjacent vertices The “old fashioned” version just has 0 for no edge and 1 for edge u v w ab 012 0  1  2  a uv w 01 2 b

14 Graphs14 Asymptotic Performance n vertices, m edges no parallel edges no self-loops Bounds are “big-Oh” Edge List Adjacency List Adjacency Matrix Space n  m n2n2 incidentEdges( v ) mdeg(v)n areAdjacent ( v, w ) mmin(deg(v), deg(w))1 insertVertex( o ) 11n2n2 insertEdge( v, w, o ) 111 removeVertex( v ) mdeg(v)n2n2 removeEdge( e ) 111

15 15 Traversals Visit each node – e.g., web crawler Have to restart traversal in each connected component, but this allows us to identify components Reachability in a digraph is an important issue – the transitive closure graph Book permits counter-direction motion in general traversals

16 What is meant by depth first search? Go deeper rather than broader Requires recursion or stack Used as a general means of traversal 16

17 Code for adjacency list implementation class Node { int ID; String nodeLabel; EdgeList adj; // successors public String toString() { return ID + nodeLabel ; } 17 class Edge { int from; // endpoint of start node int to; // endpoint of end node } class EdgeList { EdgeList next; Edge e; } public class Graph{ final static int SIZE = 20; Node [] nodes = new Node[SIZE]; }

18 At your seats Write the code to visit every node and print its name in the order it is visited. Note that with graphs, you may find yourself going in circles if you don’t mark the nodes as “visited” in some way. 18

19 19 Looking for a spanning Tree Spanning tree: all of nodes and some of edges. Like a calling tree – make sure all are informed Many times algorithms depend on some ordering of the nodes of a graph Labeling the edges is some way is helpful in organizing thinking about a graph

20 20 Depth-First Search (undirected graph) Simple recursive backtracking algorithm calls recursive function at starting point –For each incident edge to a vertex If opposite (other) vertex is unvisited –Label edge as “discovery” –Recur on the opposite vertex Else label edge as “back” Discovery edges form a component spanning tree, back edges go to an ancestor with m edges, O(m) using an adjacency list, but not using an adjacency matrix

21 Depth First Search Notice in the next diagram that multiple steps are shown between each set of pictures. Discovery edges (tree edges) are drawn with solid black lines. Back edges are drawn with dashed lines. The current node is shown in solid black 21

22 22 Depth-First Traversal

23 Given this code, find dfs numbers class Node { int ID; int dfsNum; String nodeLabel; EdgeList adj; // successors public String toString() { return ID + nodeLabel ; } 23 class Edge { int from; // endpoint of start node int to; // endpoint of end node int edgeType: {BACK, TREE, CROSS, FORWARD} } class EdgeList { EdgeList next; Edge e; } public class Graph{ final static int SIZE = 20; Node [] nodes = new Node[SIZE]; }

24 Depth-First Search24 DFS Algorithm The algorithm uses a mechanism for setting and getting “labels” of vertices and edges Algorithm DFS(G, v) Input graph G and a start vertex v of G Output labeling of the edges of G in the connected component of v as discovery edges and back edges setLabel(v, VISITED) for all e  G.incidentEdges(v) if getLabel(e)  UNEXPLORED w  G.opposite(v,e) if getLabel(w)  UNEXPLORED setLabel(e, DISCOVERY) DFS(G, w) else setLabel(e, BACK) Algorithm DFS(G) Input graph G Output labeling of the edges of G as discovery edges and back edges for all u  G.vertices() setLabel(u, UNEXPLORED) for all e  G.edges() setLabel(e, UNEXPLORED) for all v  G.vertices() if getLabel(v)  UNEXPLORED DFS(G, v)

25 Depth-First Search25 Example DB A C ED B A C ED B A C E discovery edge back edge A visited vertex A unexplored vertex unexplored edge

26 Depth-First Search26 Example (cont.) DB A C E DB A C E DB A C E D B A C E

27 Depth-First Search27 DFS and Maze Traversal The DFS algorithm is similar to a classic strategy for exploring a maze –We mark each intersection, corner and dead end (vertex) visited –We mark each corridor (edge ) traversed –We keep track of the path back to the entrance (start vertex) by means of a rope (recursion stack)

28 Depth-First Search28 Properties of DFS Property 1 DFS(G, v) visits all the vertices and edges in the connected component of v Property 2 The discovery edges labeled by DFS(G, v) form a spanning tree of the connected component of v DB A C E

29 Depth-First Search29 Complexity of DFS DFS runs in O(n  m) time provided the graph is represented by the adjacency list structure

30 30 Biconnectivity (This material replaces that in section 6.3.2) SEAPVD MIA SNA ORD FCO We worry about points of failure in the graph.

31 31 Separation Edges and Vertices Definitions –Let G be a connected graph –A separation edge of G is an edge whose removal disconnects G –A separation vertex of G is a vertex whose removal disconnects G –A graph is bi-connected if for any two vertices of the graph there are two disjoint paths between them (or a simple path with joins them) Applications –Separation edges and vertices represent single points of failure in a network and are critical to the operation of the network Example –DFW, LGA and LAX are separation vertices –(DFW,LAX) is a separation edge ORDPVD MIA DFW SFO LAX LGA HNL

32 Finding Articulation points 1. Do a depth first search, numbering the nodes as they are visited in preorder. Call the numbers num(v). If a node has already been visited, we consider the edge to it as a “back edge”. The undirected edges become “directed” by the order they are visited. 2. The root is an articulation point if it has two children. Its removal would create two subtrees. 3. For each node, compute the lowest vertex that can be reached by zero or more tree edges followed by possibly one back edge. Low(v) is the minimum of 1. Num(v) [taking no edges] 2. the lowest back edge among all back edges (v,w) [no tree edges] 3. The lowest Low(w) among all tree edges (v,w)[some tree edges and a back edge] 32

33 To compute Low, we need a postorder traversal. Given Low, we find articulation points as: 1. The root is an articulation point if it has more than one child 2. Any other vertex v is an articulation point if and only if v has some child (w) in the tree such that Low(w) >=Num(v) 33

34 In the example below, notice C is an articulation point as C has a child G and Low(G) >=Num(C). D is an articulation point as Low(E) >=Num(D). 34

35 So how do we compute Low? 35

36 What is the complexity? Assign DFS Numbers Assign Low Examine nodes for articulation point If we don’t know which is greater n (the number of nodes) or m (the number of edges), we show it as O(n+m) 36

37 37 Breadth-First Search By levels, typically using queues

38 38 BFS Facts There are discovery (tree) and cross edges (why no back edges?) – Once marked, don’t follow again. Tree edges form spanning tree Tree edges are paths, minimal in length Cross edges differ by at most one in level Try writing the code to do a BFS

39 39 Thm 6.19: Algorithms based on BFS Test for connectivity compute spanning forest compute connected components find shortest path between two points (in number of links) compute a cycle in graph, or report none (have cross edges) Good for shortest path information, while DFS better for complex connectivity questions

40 40 Digraphs A digraph is a graph whose edges are all directed –Short for “directed graph” Applications –one-way streets –flights –task scheduling Fundamental issue is reachability A C E B D

41 Complexity For each node in a digraph, how would you see which other nodes are reachable from that node? What would the complexity be? 41

42 42 Digraph Properties A graph G=(V,E) such that –Each edge goes in one direction: Edge (a,b) goes from a to b, but not b to a. If G is simple, m < n*(n-1) – at most an edge between each node and every other node. If we keep in-edges and out-edges in separate adjacency lists, we can perform listing of in-edges and out-edges in time proportional to their size. A C E B D

43 43 Digraph Application Scheduling: edge (a,b) means task a must be completed before b can be started The good life ics141 ics131 ics121 ics53 ics52 ics51 ics23ics22ics21 ics161 ics151 ics171

44 44 Digraph Facts Directed DFS gives directed paths from root to each reachable vertex Used for O(n(n+m)) algorithm [dfs is O(n+m), these algorithms use n dfs searches] –Find all induced subgraphs (from each vertex, v, find subgraph reachable from v) –Test for strong connectivity –Compute the transitive closure Directed BFS has discovery, back, cross edges

45 45 Directed DFS We can specialize the traversal algorithms (DFS and BFS) to digraphs by traversing edges only along their direction In the directed DFS algorithm, we have four types of edges –discovery edges –back edges (to ancestor) –forward edges (to descendant) –cross edges (to other) A directed DFS starting at avertex s determines the vertices reachable from s A C E B D

46 46 Reachability DFS tree rooted at v: vertices reachable from v via directed paths A C E B D F A C ED A C E B D F

47 47 Strong Connectivity Each vertex can reach all other vertices a d c b e f g

48 48 Pick a vertex v in G. Perform a DFS from v in G. –If there’s a w not visited, return not strongly connected Let G’ be G with edges reversed. Perform a DFS from v in G’. –If there’s a w not visited, return not strongly connected –Else, return strongly connected Running time: O(n+m). Strong Connectivity Algorithm G: G’: a d c b e f g a d c b e f g

49 49 Maximal subgraphs such that each vertex can reach all other vertices in the subgraph Can also be done in O(n+m) time using DFS, but is more complicated (similar to biconnectivity). Strongly Connected Components { a, c, g } { f, d, e, b } a d c b e f g

50 50 Transitive Closure Given a digraph G, the transitive closure of G is the digraph G* such that –G* has the same vertices as G –if G has a directed path from u to v (u  v), G* has a directed edge from u to v The transitive closure provides reachability information about a digraph B A D C E B A D C E G G*

51 51 Computing the Transitive Closure We can perform DFS starting at each vertex –O(n(n+m)) If there's a way to get from A to B and from B to C, then there's a way to get from A to C. Alternatively... Use dynamic programming: The Floyd-Warshall Algorithm

52 52 Floyd-Warshall Transitive Closure Idea #1: Number the vertices 1, 2, …, n. Idea #2: Consider paths that use only vertices numbered 1, 2, …, k, as intermediate vertices: k j i Uses only vertices numbered 1,…,k-1 Uses only vertices numbered 1,…,k-1 Uses only vertices numbered 1,…,k (add this edge if it’s not already in)

53 53 Floyd-Warshall’s Algorithm Floyd-Warshall’s algorithm numbers the vertices of G as v 1, …, v n and computes a series of digraphs G 0, …, G n –G 0 =G –G k has directed edge (v i, v j ) if G has directed path from v i to v j with intermediate vertices in the set {v 1, …, v k } We have that G n = G* In phase k, digraph G k is computed from G k  1 Running time: O(n 3 ), assuming areAdjacent is O(1) (e.g., adjacency matrix) Algorithm FloydWarshall(G) Input digraph G (vertices numbered) Output transitive closure G* of G G 0  G for k  1 to n do G k  G k  1 for i  1 to n (i  k) do for j  1 to n (j  i, k) do if G k  1.areAdjacent(v i v k )  G k  1.areAdjacent(v k v j ) if  G k.areAdjacent(v i v j ) G k.insertDirectedEdge(v i v j ) return G n

54 54 Floyd-Warshall Example JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6

55 55 1234567 1y 2 3yyy 4y 5yy 6yy 7yy

56 56 Floyd-Warshall, Conclusion JFK MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6 BOS

57 Recursion What if you didn’t want to use recursion to do a DFS? You could turn the parent edge around to tell you how to get back. This is termed doing a DFS “in-place” Obviously the edge would have to be flipped again after the call 57

58 58 DAGs and Topological Ordering A directed acyclic graph (DAG) is a digraph that has no directed cycles A topological ordering of a digraph is a numbering v 1, …, v n of the vertices such that for every edge (v i, v j ), we have i  j Example: in a task scheduling digraph, a topological ordering a task sequence that satisfies the precedence constraints Theorem A digraph admits a topological ordering if and only if it is a DAG B A D C E DAG G B A D C E Topological ordering of G v1v1 v2v2 v3v3 v4v4 v5v5

59 59 write c.s. program play Topological Sorting Number vertices, so that (u,v) in E implies u < v wake up eat nap study computer sci. more c.s. work out sleep dream about graphs A typical student day 1 2 3 4 5 6 7 8 9 10 11 make cookies for professors

60 60 For each vertex compute its indegree Keep a set S of all vertices with indegree 0 num = 0; While S is not empty { u = s.pop() u.number = ++num; for each of u’s successors w w.indegree— if (w.indegree ==0) S.add(w) } if (num <nodeCt) graph has a directed cycle (text differs) Algorithm for Topological Sorting

61 61 Topological Sorting DFS Algorithm Simulate the algorithm by using depth-first search O(n+m) time. Algorithm topologicalDFS(G, v) Input graph G and a start vertex v of G (having no input arcs from unvisited nodes) Output labeling of the vertices of G in the connected component of v setLabel(v,VISITED) for all edges (v,w) if getLabel(w)  !VISITED topologicalDFS(G, w) Label v with topological number n n  n - 1 Algorithm topologicalDFS(G) Input dag G Output topological ordering of G n  G.numVertices() for all u  G.vertices() setLabel(u, !VISITED) for all v  G.vertices() which have no incoming edges. if getLabel(v)  VISITED topologicalDFS(G, v)

62 62 Topological Sorting Example a f c g e i d b h

63 63 Topological Sorting Example 2 7 4 8 5 6 d 3 9

64 64 Topological Sorting Example 2 7 4 8 5 6 1 3 9


Download ppt "Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337."

Similar presentations


Ads by Google