Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Graph ADT and Algorithmic Paradigms Jeff Edmonds York University COSC 2011 Lecture 8 Graph ADT Generic Search Breadth First Search Dijkstra's Shortest.

Similar presentations


Presentation on theme: "1 Graph ADT and Algorithmic Paradigms Jeff Edmonds York University COSC 2011 Lecture 8 Graph ADT Generic Search Breadth First Search Dijkstra's Shortest."— Presentation transcript:

1 1 Graph ADT and Algorithmic Paradigms Jeff Edmonds York University COSC 2011 Lecture 8 Graph ADT Generic Search Breadth First Search Dijkstra's Shortest Paths Algorithm Depth First Search Linear Order

2 Graphs A graph is a pair (V, E), where – V is a set of nodes, called vertices – E is a collection of pairs of vertices, called edges – Vertices and edges are positions 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 Andy Mirzaian2 ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142 Last Update: Dec 4, 2014

3 Edge Types Directed edge – ordered pair of vertices (u,v) – first vertex u is the 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 – all the edges are directed – e.g., route network Undirected graph – all the edges are undirected – e.g., flight network Andy Mirzaian3 ORDPVD flight AA 1206 ORDPVD 849 miles Last Update: Dec 4, 2014

4 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 Andy Mirzaian4 Last Update: Dec 4, 2014

5 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 – U and V are adjacent Degree of a vertex – X has degree 5 Parallel edges – h and i are parallel edges Self-loop – j is a self-loop Andy Mirzaian5 XU V W Z Y a c b e d f g h i j Last Update: Dec 4, 2014

6 Terminology (cont.) Path – sequence of alternating vertices and edges – begins with a vertex – ends with a vertex – each edge is preceded and followed by its endpoints 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 Andy Mirzaian6 P1P1 XU V W Z Y a c b e d f g hP2P2 Last Update: Dec 4, 2014

7 Terminology (cont.) Cycle – circular sequence of alternating vertices and edges – each edge is preceded and followed by its endpoints Simple cycle – cycle such that all its vertices and edges are distinct 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 Andy Mirzaian7 C1C1 XU V W Z Y a c b e d f g hC2C2 Last Update: Dec 4, 2014

8 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? Andy Mirzaian8 Last Update: Dec 4, 2014

9 Vertices and Edges A graph is a collection of vertices and edges. We model the abstraction as a combination of three data types: Vertex, Edge, and Graph. A Vertex is a lightweight object that stores an arbitrary element provided by the user (e.g., an airport code) – We assume it supports a method, element(), to retrieve the stored element. An Edge stores an associated object (e.g., a flight number, travel distance, cost), retrieved with the element( ) method. Andy Mirzaian9 Last Update: Dec 4, 2014

10 Graph ADT: part 1 Andy Mirzaian 10 Last Update: Dec 4, 2014

11 Graph ADT: part 2 Andy Mirzaian 11 Last Update: Dec 4, 2014

12 Edge List Structure Andy Mirzaian12 Last Update: Dec 4, 2014 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

13 Adjacency List Structure Andy Mirzaian13 Last Update: Dec 4, 2014 Incidence sequence for each vertex – sequence of references to edge objects of incident edges Augmented edge objects – references to associated positions in incidence sequences of end vertices

14 Adjacency Map Structure Andy Mirzaian14 Last Update: Dec 4, 2014 Incidence sequence for each vertex – sequence of references to adjacent vertices, each mapped to edge object of the incident edge Augmented edge objects – references to associated positions in incidence sequences of end vertices

15 Adjacency Matrix Structure Andy Mirzaian15 Last Update: Dec 4, 2014 Edge list structure Augmented vertex objects – Integer key (index) associated with vertex 2D-array adjacency array – Reference to edge object for adjacent vertices – Null for non-adjacent vertices The “old fashioned” version just has 0 for no edge and 1 for edge

16 Performance  n vertices, m edges  no parallel edges  no self-loops Edge List Adjacency List Adjacency Matrix Spacen + 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)1 max(deg(v), deg(w)) 1 Andy Mirzaian16 Last Update: Dec 4, 2014

17 Subgraphs A subgraph S of a graph G is a graph such that – The vertices of S are a subset of the vertices of G – The edges of S are a subset of the edges of G A spanning subgraph of G is a subgraph that contains all the vertices of G Andy Mirzaian17 SubgraphSpanning subgraph Last Update: Dec 4, 2014

18 Connectivity A graph is connected if there is a path between every pair of vertices A connected component of a graph G is a maximal connected subgraph of G Andy Mirzaian18 Connected graph Non connected graph with two connected components Last Update: Dec 4, 2014

19 Trees and Forests A (free) tree is an undirected graph T such that – T is connected – T has no cycles This definition of tree is different from the one of a rooted tree A forest is an undirected graph without cycles The connected components of a forest are trees Andy Mirzaian19 Tree Forest Last Update: Dec 4, 2014

20 Spanning Trees and Forests A spanning tree of a connected graph is a spanning subgraph that is a tree A spanning tree is not unique unless the graph is a tree Spanning trees have applications to the design of communication networks A spanning forest of a graph is a spanning subgraph that is a forest Andy Mirzaian20 Graph Spanning tree Last Update: Dec 4, 2014

21 21 Graph Search

22 22 Specification: Reachability-from-single-source s : The input is a graph G (either directed or undirected) and a source node s. : Output all the nodes u that are reachable by a path in G from s. Graph Search

23 23 Graph Search Basic Steps: s u Suppose you know that u is reachable from s v & there is an edge from u to v You know that v is reachable from s Build up a set of reachable nodes.

24 24 Graph Search s reachable f u j d v w e h d b t d v w e h d b t How do we keep track of all of this information? How do we avoid cycling?

25 25 s a c h k f i l m j e b g d Graph Search

26 26 Graph Search s a c h k f i l m j e b g d

27 27 Graph Search s a c h k f i l m j e b g d

28 28 We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search l s a c h k f i l m j e b g d

29 29 We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search i.e. find its neighbors Don’t re-find a node. s a c h k f i l m j e b g d Handle some foundNotHandled node

30 30 s a c h k f i l m j e b g d We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search Handle some foundNotHandled node i.e. find its neighbors

31 31 We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search # of found nodes. measure progress # of handled nodes. Might not increase. s a c h k f i l m j e b g d

32 32 We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search Node s is foundNotHandled Other nodes notFound s a c h k f i l m j e b g d

33 33 We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search All nodes found. Exit When can’t make any more progress. No. Might not find all. When all found nodes are have been handled. Handle some foundNotHandled node s a c h k f i l m j e b g d

34 34

35 35 Graph Search We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. a c h k f i m j e b g d l Exit All found nodes are handled. Exit : Output all the nodes u that are reachable by a path in G from s. Output Found nodes

36 36 We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search Exit a c h k f i m j e b g d l All found nodes are handled. Exit Found nodes are reachable from s. Reachable nodes have been found. :

37 37 We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search Exit a c h k f i m j e b g d l All found nodes are handled. Exit Reachable nodes have been Found. : Found = handled handled notfound [A  B] = [  B   A] notFound nodes not reachable.

38 38 Graph Search Specification of Reachability-from-single-source s : The input is a graph G (either directed or undirected) and a source node s. : Output all the nodes u that are reachable by a path in G from s. EndingInitial ConditionsMake Progress Maintain Loop InvDefine Exit ConditionDefine Step Define Measure of Progress Define Loop Invariants Define Problem 79 km to school Exit 79 km75 km Exit 0 kmExit

39 39 Graph Search # of handled nodes. Handle some foundNotHandled node Time = O(n) f u j O(n) iterations, but iteration takes more than O(1) O(n) neighbors = O(n 2 ) Could be fewer? Each edge visited, times.2 = O(E) Size =O(E) Linear time.

40 40 Graph Search Which foundNotHandled node do we handle? Queue: –Handle node Found longest ago Likely closest to s (in # of edges). –Breadth-First Search Priority Queue: –Handle node that seems to be closest to s (weighted). –Dijkstra's Shortest-Weighted Paths Stack: –Handle node Found most recently Likely farthest from s. –Depth-First Search

41 41 Graph Search Which foundNotHandled node do we handle? Queue: –Handle node Found longest ago Likely closest to s (in # of edges). –Breadth-First Search So far, the nodes have been found in order of length from s.

42 42 BFS s a c h k f i l m j e b g d Found Not Handled Queue

43 43 BFS s a c h k f i l m j e b g d Found Not Handled Queue s d=0

44 44 BFS Found Not Handled Queue d=0 a b g d d=1 s a c h k f i l m j e b g d d=0 d=1

45 45 BFS s a c h k f i l m j e b g d Found Not Handled Queue a b g d d=0 d=1

46 46 BFS s a c h k f i l m j e b g d Found Not Handled Queue b g d c f d=0 d=1 d=2 d=1 d=2

47 47 BFS s a c h k f i l m j e b g d Found Not Handled Queue b g c f m e d=0 d=1 d=2 d=1 d=2

48 48 BFS s a c h k f i l m j e b g d Found Not Handled Queue d=0 d=1 d=2 b j c f m e d=1 d=2

49 49 BFS s a c h k f i l m j e b g d Found Not Handled Queue d=0 d=1 d=2 j c f m e d=1 d=2

50 50 BFS s a c h k f i l m j e b g d Found Not Handled Queue c f m e j d=0 d=1 d=2

51 51 BFS s a c h k f i l m j e b g d Found Not Handled Queue f m e j h i d=0 d=1 d=2 d=3 d=2 d=3

52 52 BFS s a c h k f i l m j e b g d Found Not Handled Queue m e j h i d=0 d=1 d=2 d=3 d=2 d=3

53 53 BFS s a c h k f i l m j e b g d Found Not Handled Queue e j h i l d=0 d=1 d=2 d=3 d=2 d=3

54 54 BFS s a c h k f i l m j e b g d Found Not Handled Queue j h i l d=0 d=1 d=2 d=3 d=2 d=3

55 55 BFS s a c h k f i l m j e b g d Found Not Handled Queue h i l d=0 d=1 d=2 d=3 d=2 d=3

56 56 BFS s a c h k f i l m j e b g d Found Not Handled Queue h d=0 d=1 d=2 d=3 i l

57 57 BFS s a c h k f i l m j e b g d Found Not Handled Queue i l k d=0 d=1 d=2 d=3 d=4 d=3 d=4

58 58 BFS s a c h k f i l m j e b g d Found Not Handled Queue l k d=0 d=1 d=2 d=3 d=4 d=3 d=4

59 59 BFS s a c h k f i l m j e b g d Found Not Handled Queue k d=0 d=1 d=2 d=3 d=4 d=3 d=4

60 60 BFS s a c h k f i l m j e b g d Found Not Handled Queue k d=0 d=1 d=2 d=3 d=4

61 61 BFS s a c h k f i l m j e b g d Found Not Handled Queue d=0 d=1 d=2 d=3 d=4 d=5

62 62 BFS So far, the nodes have been found in order of length from s.

63 63 BFS So far, the nodes have been found in order of length from s. : Finds a shortest path from s to each node v and its length. When we find v, we know there isn't a shorter path to it because ? Otherwise, we would have found it already.

64 64 BFS Data structure for storing a path to each node: For each node v, store  (v) to be parent of v.

65 65 Basic Steps: s u The shortest path to u has length d v & there is an edge from u to v There is a path to v with length d+1. BFS Parent of v is  (v) = u.

66 66

67 67 Graph Search Which foundNotHandled node do we handle? Queue: –Handle node Found longest ago Likely closest to s (in # of edges). –Breadth-First Search Priority Queue: –Handle node that seems to be closest to s (weighted). –Dijkstra's Shortest-Weighted Paths Stack: –Handle node Found most recently Likely farthest from s. –Depth-First Search

68 68 Dijkstra's Shortest-Weighted Paths Specification: Dijkstra's Shortest-Weighted Paths Reachability-from-single-source s : The input is a graph G (either directed or undirected) with positive edge weights and a source node s. : Finds a shortest weighted path from s to each node v and its length.

69 69 Dijkstra's Shortest-Weighted Paths The king wanted to know the shortest path from his castle s to each node (home) v. So that people could come and go quickly and so that he could guard these home and paths. s D( ) = length = 8. 5 3  ( ) = previous node in path

70 70 Dijkstra's Shortest-Weighted Paths Rome was not built in a day. He decided to handle the nodes in terms of their distance from s. He put a wall around these handled nodes. s

71 71 Dijkstra's Shortest-Weighted Paths We have the answer for handled nodes (i.e. closest) i.e. D(u) and  (u) give shortest path from s to u. Exit When all the nodes are handled, done! s

72 72 Dijkstra's Shortest-Weighted Paths When a node has been handled: He put a knight on each handled node to guard all edges (roads) leading out of the node. An edge is handled if it comes out of a handled node. i.e. all edges in and leaving city. s

73 73 Dijkstra's Shortest-Weighted Paths Unhandled edges are unguarded, so we don’t want to travel along them, even if they make a shorter path. s

74 74 Dijkstra's Shortest-Weighted Paths s A path is handled if has handled edges. i.e. path travels from s between the handled nodes within the walls and then one last edge out at gate. For unhandled nodes,  (u) and d(u) values give the shortest handled path from s. d( )=8. d( )=20. d( )= 

75 75 Dijkstra's Shortest-Weighted Paths s d( )=8. 5 d( ) + w(, ) = 8 + 5 = 13 This red path to has length This is better but unhandled and hence unguarded. d( )=20.

76 76 Dijkstra's Shortest-Weighted Paths s d( )=8. 5 Theorem: If has the smallest d value, then its shortest path from s is handled. Proof: Any unhandled path must leave at a different gate, and hence already has gone further than d( ). d( )=20. D

77 77 Dijkstra's Shortest-Weighted Paths s D( )=8. 5 Consider the unhandled node with the smallest d( ). d( )=20. Priority Queue (FoundNotHandled): Handle node that seems to be closest to s.

78 78 Dijkstra's Shortest-Weighted Paths s D( )=8. 5 Consider the unhandled node with the smallest d( ). Handled it. Handle out going edges. Update its neighbor’s d values. d( )=20.

79 79 Dijkstra's Shortest-Weighted Paths s D( )=8. 5 d( ) + w(, ) = 8 + 5 = 13 This red path to has length This is better but was unhandled. d( )=20. But with handled, this edge is now handled so we are good.

80 80 Dijkstra's Shortest-Weighted Paths s D( )=8. 5 d( ) + w(, ) = 8 + 5 = 13 This red path to has length d( )=20. But with handled, this edge is now handled so we are good. 13  ( ) This is better but was unhandled.

81 81 Dijkstra's Shortest-Weighted Paths s D( )=8. 5 d( )=13.  ( ) We have the answer for handled nodes (i.e. closest) i.e. D(u) and  (u) give shortest path from s to u. Done!

82 82 Dijkstra's Shortest-Weighted Paths s D( )=8. 5 d( )=13.  ( ) Done! For unhandled nodes,  (u) and d(u) values give the shortest handled path from s.

83 83 Dijkstra's Shortest-Weighted Paths s D( )=8. 5 d( )=13.  ( ) EndingInitial ConditionsMake Progress Maintain Loop Inv Define Exit Condition Define Step Define Measure of Progress Define Loop Invariants Define Problem 79 km to school Exit 79 km75 km Exit 0 kmExit Done!

84 84

85 85 Dijkstra's u v s Handled nodesFound nodes Handled Edges? Definition of handled paths and d(v)

86 86 Dijkstra's u v s Handled nodesFound nodes Handled Edges Handled Paths? Definition of handled paths and d(v)

87 87 Dijkstra's u v s Handled nodesFound nodes Definition of handled paths and d(v) Handled Edges Handled Paths?

88 88 Dijkstra's u v s Handled nodes d(v) is the length of the shortest handled paths to v. For handled u, d(u) is the length of the shortest paths to u. NotFound d(v’)=d(v’)=  Definition of handled paths and d(v)

89 89 u The shortest of handled paths to u has length d(u) Dijkstra's & there is an edge from u to v w<u,v>w<u,v> v s The shortest of handled paths to v has length d(v) The shortest handled path to v has length min( d(v), d(u)+w ). Handle node that “seems” to be closest to s. Basic Steps:

90 90 s c b Dijkstra's a d f ij h e g 40 1 10 2 15 1 2 6 8 1 2 30 3 d=d= d=d= d=d= d=d= d=d= d=d= d=d= d=0d=0 d=d= d=d= d=d= 1 2 3 3

91 91 s c b Dijkstra's a d f ij h e g 40 1 10 2 1 6 8 1 2 30 3 d=1d=1 d=d= d=d= d=d= d=d= d=10 d=0d=0 d=40 d=d= d=30 30 1 15 2 2 3 d=d= 3

92 92 s c b Dijkstra's a d f ij h e g 40 1 10 2 1 1 6 8 1 2 30 3 d=1d=1 d=3d=3 d=d= d=d= d=d= d=d= d=10 d=0d=0 d=31 d=d= d=30 30 15 2 2 3 3

93 93 s c b Dijkstra's a d f ij h e g 40 1 10 2 1 1 6 8 1 2 30 3 d=1d=1 d=3d=3 d=d= d=d= d=d= d=d= d=10 d=0d=0 d=31 d=4d=4 d=18 30 15 2 2 3 3

94 94 s c b Dijkstra's a d f ij h e g 40 1 10 2 1 1 6 8 1 2 30 3 d=1d=1 d=3d=3 d=d= d=d= d=10 d=d= d=0d=0 d=6d=6 d=4d=4 d=5d=5 30 1 15 2 3 3

95 95 s c b Dijkstra's a d f ij h e g 40 1 10 2 1 1 6 8 1 2 30 3 d=1d=1 d=3d=3 d=d= d=d= d=7d=7 d=d= d=10 d=0d=0 d=6d=6 d=4d=4 d=5d=5 30 1 15 2 3 3

96 96 s c b Dijkstra's a d f ij h e g 40 1 10 2 1 1 6 8 1 2 30 3 d=1d=1 d=3d=3 d=d= d=d= d=7d=7 d=d= d=9d=9 d=0d=0 d=6d=6 d=4d=4 d=5d=5 1 15 2 3 3

97 97 s c b Dijkstra's a d f ij h e g 40 1 10 2 1 1 6 8 1 2 30 3 d=1d=1 d=3d=3 d=d= d=d= d=7d=7 d=15 d=8d=8 d=0d=0 d=6d=6 d=4d=4 d=5d=5 30 1 15 2 3 3

98 98 s c b Dijkstra's a d f ij h e g 40 1 10 2 1 1 6 8 1 2 30 3 d=1d=1 d=3d=3 d=d= d=d= d=7d=7 d=10 d=8d=8 d=0d=0 d=6d=6 d=4d=4 d=5d=5 30 1 15 2 3 3

99 99 s c b Dijkstra's a d f ij h e g 40 1 10 2 1 1 6 8 1 2 30 3 d=1d=1 d=3d=3 d=d= d=d= d=7d=7 d=10 d=8d=8 d=0d=0 d=6d=6 d=4d=4 d=5d=5 30 1 15 2 3 3

100 100 s c b Dijkstra's a d f ij h e g 40 1 10 2 1 1 6 8 1 2 30 3 d=1d=1 d=3d=3 d=d= d=d= d=7d=7 d=10 d=8d=8 d=0d=0 d=6d=6 d=4d=4 d=5d=5 30 1 15 2 3 3

101 101 s c b Dijkstra's a d f ij h e g 40 1 10 2 1 1 6 8 1 2 30 3 d=1d=1 d=3d=3 d=d= d=d= d=7d=7 d=10 d=8d=8 d=0d=0 d=6d=6 d=4d=4 d=5d=5 30 1 15 2 3 DONE 3

102 102 Dijkstra's 0 13 16 17 20 ∞ Handle c 19 Π(d) =c Π(e) =c

103 103 Time = O(E) Each edge visited, times. 2 ? Dijkstra's This number of times following an edge. Must update Priority Queue. Takes time O(log(n)) Time = O(E log(n)) For each update, d(v) = min( d(v), d(u)+w ). Heap

104 104 Adaptable Heap Pop/Push/Changes But now it is not a heap! The 39 “bubbles” down or up until it finds its spot. Suppose some outside user knows about some data item c and remembers where it is in the heap. And changes its priority from 21 to 39 21 c 39 27

105 105 Adaptable Heap Pop/Push/Changes But now it is not a heap! The 39 “bubbles” down or up until it finds its spot. Suppose some outside user also knows about data item f and its location in the heap just changed. The Heap must be able to find this outside user and tell him it moved. 21 c 27 39 27 f Time = O(log n)

106 106 Heap Implementation A location-aware heap entry is an object storing  key  value  position of the entry in the underlying heap In turn, each heap position stores an entry Back pointers are updated during entry swaps Last Update: Oct 23, 2014 Andy106 4 a 2 d 6 b 8 g 5 e 9 c

107 107 Dijkstra's

108 108 Dijkstra's

109 109 Graph Search Which foundNotHandled node do we handle? Queue: –Handle node Found longest ago Likely closest to s (in # of edges). –Breadth-First Search Priority Queue: –Handle node that seems to be closest to s (weighted). –Dijkstra's Shortest-Weighted Paths Stack: –Handle node Found most recently Likely farthest from s. –Depth-First Search

110 110 DFS Breadth first search makes a lot of sense for dating in general actually. It suggests dating a bunch of people casually before getting serious rather than having a series of five year relationships.

111 111 DFS s a c h k f i l m j e b g d Found Not Handled Stack

112 112 DFS s a c h k f i l m j e b g d Found Not Handled Stack s

113 113 DFS s a c h k f i l m j e b g d Found Not Handled Stack a b g d

114 114 DFS s a c h k f i l m j e b g d Found Not Handled Stack a m g d e

115 115 DFS

116 116 DFS The nodes in the stack form a path starting at s.

117 117 DFS s a c h k f i l m j e b g d Found Not Handled Stack

118 118 DFS s a c h k f i l m j e b g d Found Not Handled Stack s,0

119 119 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,0

120 120 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,0

121 121 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,1 h,0

122 122 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,1 h,1 k,0

123 123 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,1 h,1 Tree Edge Path on Stack

124 124 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,1

125 125 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,0

126 126 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,1 Cross Edge to handled node

127 127 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,2

128 128 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,3 l,0

129 129 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,3 l,1

130 130 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,3

131 131 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,4 g,0

132 132 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,4 g,1 j,0

133 133 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,4 g,1 j,1 Back Edge to node on Stack Forms a cycle

134 134 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,4 g,1 j,2 m,0

135 135 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,4 g,1 j,2 m,1

136 136 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,4 g,1 j,2

137 137 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,4 g,1

138 138 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,4

139 139 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,5 f,0

140 140 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,5 f,1

141 141 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2 i,5

142 142 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,2

143 143 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,3

144 144 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1

145 145 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,2

146 146 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack

147 147 DFS s a c h k f i l m j e b g d s,2 Found Not Handled Stack d,1

148 148 DFS s a c h k f i l m j e b g d s,2 Found Not Handled Stack d,2

149 149 DFS s a c h k f i l m j e b g d s,2 Found Not Handled Stack d,3 e,0

150 150 DFS s a c h k f i l m j e b g d s,2 Found Not Handled Stack d,3 e,1

151 151 DFS s a c h k f i l m j e b g d s,2 Found Not Handled Stack d,3

152 152 DFS s a c h k f i l m j e b g d s,2 Found Not Handled Stack

153 153 DFS s a c h k f i l m j e b g d s,3 Found Not Handled Stack

154 154 DFS s a c h k f i l m j e b g d s,4 Found Not Handled Stack b,0

155 155 DFS s a c h k f i l m j e b g d s,4 Found Not Handled Stack b,1

156 156 DFS s a c h k f i l m j e b g d s,4 Found Not Handled Stack b,2

157 157 DFS s a c h k f i l m j e b g d s,4 Found Not Handled Stack b,3

158 158 DFS s a c h k f i l m j e b g d s,4 Found Not Handled Stack

159 159 DFS s a c h k f i l m j e b g d Found Not Handled Stack done

160 160

161 161 Recursive Depth First Search Get help from friends

162 162

163 163 Linear Order underwear pants socks shoes underwear pants socks shoes socks underwear pants shoes

164 164 Linear Order underwear pants socks shoes ?

165 165 Linear Order a b h c i d j e k f l g : A Directed Acyclic Graph (DAG) : Find one valid linear order ….. l Algorithm: Find a sink. Put it last in order. Delete & Repeat ?

166 166 Linear Order a b h c i d j e k f l g : A Directed Acyclic Graph (DAG) : Find one valid linear order (n)(n2)(n)(n2) Algorithm: Find a sink. Put it last in order. Delete & Repeat ….. l

167 167 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS d e g f l ….. f

168 168 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS d e g l l When take off stack add to backwards order ….. f

169 169 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS d e g l When take off stack add to backwards order l,f

170 170 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS d e l When take off stack add to backwards order g,l,f

171 171 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS d l When take off stack add to backwards order e,g,l,f

172 172 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS l When take off stack add to backwards order d,e,g,l,f

173 173 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS i l When take off stack add to backwards order d,e,g,l,f j k

174 174 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS i l When take off stack add to backwards order k,d,e,g,l,f j

175 175 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS i l When take off stack add to backwards order j,k,d,e,g,l,f

176 176 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS l When take off stack add to backwards order i,j,k,d,e,g,l,f

177 177 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS b l When take off stack add to backwards order c i,j,k,d,e,g,l,f

178 178 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS b l When take off stack add to backwards order c,i,j,k,d,e,g,l,f

179 179 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS l When take off stack add to backwards order b,c,i,j,k,d,e,g,l,f

180 180 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS a l When take off stack add to backwards order h b,c,i,j,k,d,e,g,l,f

181 181 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS a l When take off stack add to backwards order h,b,c, i,j,k,d,e,g,l,f

182 182 Linear Order a b h c i d j e k f g Found Not Handled Stack Alg: DFS l When take off stack add to backwards order a,h,b,c,i,j,k,d,e,g,l,f done

183 183 Linear Order Found Not Handled Stack Proof: Case 1: u goes on stack first before v. Because of edge, v goes on before u comes off v comes off before u comes off v goes after u in order. u v v…u… Consider each edge v … u …

184 184 Linear Order Found Not Handled Stack Proof: Case 1: u goes on stack first before v. Case 2: v goes on stack first before u. v comes off before u goes on. v goes after u in order. u v v…u… Consider each edge u … v …

185 185 Linear Order Found Not Handled Stack Proof: Case 1: u goes on stack first before v. Case 2: v goes on stack first before u. v comes off before u goes on. Case 3: v goes on stack first before u. u goes on before v comes off. Panic: u goes after v in order.  Cycle means linear order is impossible u v u…v… Consider each edge u … v … The nodes in the stack form a path starting at s. done

186 186 Ingredients: Instances: The possible inputs to the problem. Solutions for Instance: Each instance has an exponentially large set of solutions. Cost of Solution: Each solution has an easy to compute cost or value. Specification : The input is one instance. : A valid solution with optimal cost. (minimum or maximum) Optimization Problems

187 187 End


Download ppt "1 Graph ADT and Algorithmic Paradigms Jeff Edmonds York University COSC 2011 Lecture 8 Graph ADT Generic Search Breadth First Search Dijkstra's Shortest."

Similar presentations


Ads by Google