Download presentation
Presentation is loading. Please wait.
1
Graph ADT and Algorithmic Paradigms
Generic Search Breadth First Search Dijkstra's Shortest Paths Algorithm Depth First Search Linear Order Jeff Edmonds York University Lecture 8 COSC 2011
2
Graphs A graph is a pair (V, E), where Example: PVD ORD SFO LGA HNL
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 ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142 Last Update: Dec 4, 2014 Andy Mirzaian
3
Edge Types Directed edge Undirected edge Directed graph
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 flight AA 1206 ORD PVD 849 miles ORD PVD Last Update: Dec 4, 2014 Andy Mirzaian
4
Applications Electronic circuits Transportation networks
Printed circuit board Integrated circuit Transportation networks Highway network Flight network Computer networks Local area network Internet Web Databases Entity-relationship diagram Last Update: Dec 4, 2014 Andy Mirzaian
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 X U V W Z Y a c b e d f g h i j Last Update: Dec 4, 2014 Andy Mirzaian
6
Terminology (cont.) Path Simple path Examples: P1 X U V W Z Y a c b e
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: P1 = (V,b,X,h,Z) is a simple path P2 = (U,c,W,e,X,g,Y,f,W,d,V) is a path that is not simple P1 X U V W Z Y a c b e d f g h P2 Last Update: Dec 4, 2014 Andy Mirzaian
7
Terminology (cont.) Cycle Simple cycle Examples: C1 X U V W Z Y a c b
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: C1 = (V,b,X,g,Y,f,W,c,U,a,) is a simple cycle C2 = (U,c,W,e,X,g,Y,f,W,d,V,a,) is a cycle that is not simple C1 X U V W Z Y a c b e d f g h C2 Last Update: Dec 4, 2014 Andy Mirzaian
8
Properties v deg(v) = 2m Notation Property 1 Property 2
n number of vertices m number 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? Last Update: Dec 4, 2014 Andy Mirzaian
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. Last Update: Dec 4, 2014 Andy Mirzaian
10
Graph ADT: part 1 Last Update: Dec 4, 2014 Andy Mirzaian
11
Graph ADT: part 2 Last Update: Dec 4, 2014 Andy Mirzaian
12
Edge List Structure Vertex object Edge object Vertex sequence
element reference to position in vertex sequence Edge object origin vertex object destination vertex object reference to position in edge sequence Vertex sequence sequence of vertex objects Edge sequence sequence of edge objects Last Update: Dec 4, 2014 Andy Mirzaian
13
Adjacency List Structure
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 Last Update: Dec 4, 2014 Andy Mirzaian
14
Adjacency Map Structure
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 Last Update: Dec 4, 2014 Andy Mirzaian
15
Adjacency Matrix Structure
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 Last Update: Dec 4, 2014 Andy Mirzaian
16
Performance Edge List Adjacency List Adjacency Matrix Space n + m n2
n vertices, m edges no parallel edges no self-loops Edge List Adjacency List Adjacency Matrix Space n + m n2 incidentEdges(v) m deg(v) n areAdjacent (v, w) min(deg(v), deg(w)) 1 insertVertex(o) insertEdge(v, w, o) removeVertex(v) removeEdge(e) max(deg(v), deg(w)) Last Update: Dec 4, 2014 Andy Mirzaian
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 Subgraph Spanning subgraph Last Update: Dec 4, 2014 Andy Mirzaian
18
Non connected graph with two connected components
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 Non connected graph with two connected components Connected graph Last Update: Dec 4, 2014 Andy Mirzaian
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 Forest Tree Last Update: Dec 4, 2014 Andy Mirzaian
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 Spanning tree Graph Last Update: Dec 4, 2014 Andy Mirzaian
21
Graph Search
22
Graph Search Specification: Reachability-from-single-source s
<preCond>: The input is a graph G (either directed or undirected) and a source node s. <postCond>: Output all the nodes u that are reachable by a path in G from s.
23
Graph Search Basic Steps: s v & there is an edge from u to v u
Suppose you know that u is reachable from s You know that v is reachable from s Build up a set of reachable nodes.
24
Graph Search reachable b t b t reachable e h d e h d reachable d v w d
f u j s How do we keep track of all of this information? How do we avoid cycling?
25
Graph Search s b a d e g c f j i h m k l
26
Graph Search s b a d e g c f j i h m k l
27
Graph Search s b a d e g c f j i h m k l
28
Graph Search s We know found nodes are reachable from s because we have traced out a path. b a d e g c f If a node has been handled, then all of its neighbors have been found. j i h m k l l
29
Graph Search s We know found nodes are reachable from s because we have traced out a path. b a d e g Handle some foundNotHandled node c f If a node has been handled, then all of its neighbors have been found. j i h m k l i.e. find its neighbors Don’t re-find a node.
30
Graph Search s We know found nodes are reachable from s because we have traced out a path. b a d e g c f j If a node has been handled, then all of its neighbors have been found. i h m k l Handle some foundNotHandled node i.e. find its neighbors
31
Graph Search s We know found nodes are reachable from s because we have traced out a path. b a d e g c f If a node has been handled, then all of its neighbors have been found. j i h m k l measure progress # of found nodes. Might not increase. # of handled nodes.
32
Graph Search s We know found nodes are reachable from s because we have traced out a path. b a d e g c f If a node has been handled, then all of its neighbors have been found. j i h m k l Node s is foundNotHandled Other nodes notFound
33
Graph Search s We know found nodes are reachable from s because we have traced out a path. b a d e g c f If a node has been handled, then all of its neighbors have been found. j i h m k l Exit All nodes found. No. Might not find all. When can’t make any more progress. Handle some foundNotHandled node When all found nodes are have been handled.
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. b a d e g c f j i h m k l Exit All found nodes are handled. <postCond>: Output all the nodes u that are reachable by a path in G from s. Exit Output Found nodes
36
Graph Search We know found nodes are reachable from s because we have traced out a path. b a d e g c f j If a node has been handled, then all of its neighbors have been found. i h m k l Exit All found nodes are handled. <postCond>: Exit Found nodes are reachable from s. Reachable nodes have been found.
37
Graph Search Exit Found = handled
We know found nodes are reachable from s because we have traced out a path. b a d e g c f j If a node has been handled, then all of its neighbors have been found. i h handled notfound m k l Exit All found nodes are handled. <postCond>: Exit Reachable nodes have been Found. [A B] = [B A] notFound nodes not reachable.
38
Graph Search Specification of Reachability-from-single-source s
<preCond>: The input is a graph G (either directed or undirected) and a source node s. <postCond>: Output all the nodes u that are reachable by a path in G from s. Ending Initial Conditions Make Progress Maintain Loop Inv Define Exit Condition Define Step Define Measure of Progress Define Loop Invariants Define Problem 79 km to school Exit 75 km 0 km
39
Graph Search # of handled nodes. Handle some foundNotHandled node f u
j O(n) iterations, but iteration takes more than O(1) Time = O(n) O(n) neighbors = O(n2) Could be fewer? Each edge visited, times. 2 = O(E) Linear time. Size = O(E)
40
Graph Search Stack: 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: Found most recently Likely farthest from s. Depth-First Search
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
Found Not Handled Queue
BFS Found Not Handled Queue s b a e d g c f j i h m k l
43
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s b a s d=0 e d g c f j i h m k l
44
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=0 e a d=1 d d g g c f b j i h m k l
45
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a a d=1 d e d g g b c f j i h m k l
46
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=1 d e d g g b c f c d=2 j f d=2 i h m k l
47
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=1 e d g g b c f c d=2 j f d=2 m i e h m k l
48
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=1 e d g b c f c d=2 j f d=2 m i e h j m k l
49
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=1 e d g c f c d=2 j f d=2 m i e h j m k l
50
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a c d=2 e d f m g e c f j j d=2 i h m k l
51
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=2 e d f m g e c f j j h d=3 d=2 i i h m d=3 k l
52
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=2 e d m g e c f j j h d=3 d=2 i i h m d=3 k l
53
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=2 e d g e c f j j h d=3 d=2 i i h l m d=3 k l
54
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=2 e d g c f j j h d=3 d=2 i i h l m d=3 k l
55
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=2 e d g c f j h d=3 d=2 i i h l m d=3 k l
56
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a h d=3 i e d l g c f j d=2 i h m d=3 k l
57
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=3 i e d l g k d=4 c f j d=2 i h m d=3 k d=4 l
58
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=3 e d l g k d=4 c f j d=2 i h m d=3 k d=4 l
59
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=3 e d g k d=4 c f j d=2 i h m d=3 k d=4 l
60
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a k d=4 e d g c f j d=2 i h m d=3 k d=4 l
61
Found Not Handled Queue
BFS Found Not Handled Queue d=0 s d=1 b a d=4 e d=5 d g c f j d=2 i h m d=3 k d=4 l
62
BFS So far, the nodes have been found in order of length from s.
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. <postCond>: When we find v, we know there isn't a shorter path to it because ? Otherwise, we would have found it already.
64
BFS Data structure for storing a path to each node:
For each node v, store (v) to be parent of v.
65
BFS 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. Parent of v is (v) = u.
67
Graph Search Stack: 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: Found most recently Likely farthest from s. Depth-First Search
68
Dijkstra's Shortest-Weighted Paths
Specification: Dijkstra's Shortest-Weighted Paths Reachability-from-single-source s <preCond>: The input is a graph G (either directed or undirected) with positive edge weights and a source node s. <postCond>: Finds a shortest weighted path from s to each node v and its length.
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. 5 3 ( ) = previous node in path D( ) = length = 8.
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.
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!
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.
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.
74
Dijkstra's Shortest-Weighted Paths
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( )= d( )=8. d( )=20.
75
Dijkstra's Shortest-Weighted Paths
This red path to has length d( ) + w( , ) = = This is better but unhandled and hence unguarded. 5 d( )=8. d( )=20.
76
Dijkstra's Shortest-Weighted Paths
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( ). 5 d( )=8. D d( )=20.
77
Dijkstra's Shortest-Weighted Paths
Consider the unhandled node with the smallest d( ). Priority Queue (FoundNotHandled): Handle node that seems to be closest to s. 5 D( )=8. d( )=20.
78
Dijkstra's Shortest-Weighted Paths
Consider the unhandled node with the smallest d( ). Handled it. Handle out going edges. Update its neighbor’s d values. 5 D( )=8. d( )=20.
79
Dijkstra's Shortest-Weighted Paths
This red path to has length d( ) + w( , ) = = This is better but was unhandled . But with handled, this edge is now handled so we are good. 5 D( )=8. d( )=20.
80
Dijkstra's Shortest-Weighted Paths
This red path to has length d( ) + w( , ) = = This is better but was unhandled . But with handled, this edge is now handled so we are good. 5 D( )=8. ( ) d( )=20. 13
81
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. Done! 5 D( )=8. ( ) d( )=13.
82
Dijkstra's Shortest-Weighted Paths
For unhandled nodes, (u) and d(u) values give the shortest handled path from s. Done! 5 D( )=8. ( ) d( )=13.
83
Dijkstra's Shortest-Weighted Paths
Ending Initial Conditions Make Progress Maintain Loop Inv Define Exit Condition Define Step Define Measure of Progress Define Loop Invariants Define Problem 79 km to school Exit 75 km 0 km Done! 5 D( )=8. ( ) d( )=13.
85
Definition of handled paths and d(v)
Dijkstra's Definition of handled paths and d(v) Handled nodes Found nodes s v u Handled Edges?
86
Definition of handled paths and d(v)
Dijkstra's Definition of handled paths and d(v) Handled nodes Found nodes s v u Handled Edges Handled Paths?
87
Definition of handled paths and d(v)
Dijkstra's Definition of handled paths and d(v) Handled nodes Found nodes s v u Handled Edges Handled Paths?
88
Dijkstra's Definition of handled paths and d(v) Handled nodes s v
NotFound d(v’)= u For handled u, d(u) is the length of the shortest paths to u. d(v) is the length of the shortest handled paths to v.
89
Dijkstra's Basic Steps: Handle node that “seems” to be closest to s. v
The shortest of handled paths to v has length d(v) u The shortest of handled paths to u has length d(u) & there is an edge from u to v w<u,v> The shortest handled path to v has length min( d(v), d(u)+w<u,v> ).
90
Dijkstra's s b a d e c f h g i j d=0 10 1 d= d= 30 2 30 40 3 3 1 2
8 15 1 2 f h d= 1 d= g 6 d= i 3 j d= d=
91
Dijkstra's s b a d e c f h g i j d=0 10 1 d=10 d=1 30 2 30 40 3 3 1 2
8 15 1 2 f h d= 1 d= g 6 d= i 3 j d= d=
92
Dijkstra's s b a d e c f h g i j d=0 10 1 d=10 d=1 30 2 30 40 3 3 1 2
8 15 1 2 f h d=3 1 d= g 6 d= i 3 j d= d=
93
Dijkstra's s b a d e c f h g i j d=0 10 1 d=10 d=1 30 2 30 40 3 3 1 2
8 15 1 2 f h d=3 1 d= g 6 d=4 i 3 j d= d=
94
Dijkstra's s b a d e c f h g i j d=0 10 1 d=10 d=1 30 2 30 40 3 3 1 2
8 15 1 1 f h d=3 1 d=10 g 6 d=4 i 3 j d= d=
95
Dijkstra's s b a d e c f h g i j d=0 10 1 d=10 d=1 30 2 30 40 3 3 1 2
8 15 1 1 f h d=3 1 d=7 g 6 d=4 i 3 j d= d=
96
Dijkstra's s b a d e c f h g i j d=0 10 1 d=9 d=1 30 2 30 40 3 3 1 2
8 15 1 1 f h d=3 1 d=7 g 6 d=4 i 3 j d= d=
97
Dijkstra's s b a d e c f h g i j d=0 10 1 d=8 d=1 30 2 30 40 3 3 1 2
15 1 1 f h d=3 1 d=7 g 6 d=4 i 3 j d= d=
98
Dijkstra's s b a d e c f h g i j d=0 10 1 d=8 d=1 30 2 30 40 3 3 1 2
15 1 1 f h d=3 1 d=7 g 6 d=4 i 3 j d= d=
99
Dijkstra's s b a d e c f h g i j d=0 10 1 d=8 d=1 30 2 30 40 3 3 1 2
15 1 1 f h d=3 1 d=7 g 6 d=4 i 3 j d= d=
100
Dijkstra's s b a d e c f h g i j d=0 10 1 d=8 d=1 30 2 30 40 3 3 1 2
15 1 1 f h d=3 1 d=7 g 6 d=4 i 3 j d= d=
101
Dijkstra's s b a d e c f h g i j d=0 10 1 d=8 d=1 30 2 30 40 3 3 1 2
15 1 1 f h d=3 1 d=7 g 6 d=4 DONE i 3 j d= d=
102
Dijkstra's 13 16 19 37 20 ∞ 17 Π(e) =c Π(d) =c Handle c
103
Dijkstra's Each edge visited, times. 2 Time = O(E) ?
This number of times following an edge. For each update, d(v) = min( d(v), d(u)+w<u,v> ). Must update Priority Queue. Takes time O(log(n)) Heap Time = O(E log(n))
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 27 39 21 39 21 c
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. 39 27 39 27 f 21 c Time = O(log n)
106
Heap Implementation A location-aware heap entry is an object storing
4 a 2 d 6 b 8 g 5 e 9 c 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 Andy
107
Dijkstra's
108
Dijkstra's
109
Graph Search Stack: 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: Found most recently Likely farthest from s. Depth-First Search
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
Found Not Handled Stack
DFS Found Not Handled Stack s b a e d g c f j i h m k l
112
Found Not Handled Stack
DFS Found Not Handled Stack s b a e d g c f j i s h m k l
113
Found Not Handled Stack
DFS Found Not Handled Stack s b a e d g c f b j g i d a h m k l
114
Found Not Handled Stack
DFS Found Not Handled Stack s b a e d g c e f m j g i d a h m k l
115
DFS
116
DFS The nodes in the stack form a path starting at s.
117
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j i h m k l
118
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j i s,0 h m k l
119
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j i a,0 s,1 h m k l
120
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j c,0 i a,1 s,1 h m k l
121
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f h,0 j c,1 i a,1 s,1 h m k l
122
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c k,0 f h,1 j c,1 i a,1 s,1 h m k l
123
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f h,1 j c,1 Path on Stack i a,1 s,1 h m Tree Edge k l
124
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j c,1 i a,1 s,1 h m k l
125
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f i,0 j c,2 i a,1 s,1 h m k l
126
DFS Found Not Handled Stack <node,# edges> s b a e d g c f i,1 j
m Cross Edge to handled node k l
127
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f i,2 j c,2 i a,1 s,1 h m k l
128
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c l,0 f i,3 j c,2 i a,1 s,1 h m k l
129
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c l,1 f i,3 j c,2 i a,1 s,1 h m k l
130
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f i,3 j c,2 i a,1 s,1 h m k l
131
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c g,0 f i,4 j c,2 i a,1 s,1 h m k l
132
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g j,0 c g,1 f i,4 j c,2 i a,1 s,1 h m k l
133
DFS Found Not Handled Stack <node,# edges> s b a e d g j,1 c g,1
i,4 j c,2 i a,1 s,1 h Back Edge to node on Stack m k l Forms a cycle
134
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d m,0 g j,2 c g,1 f i,4 j c,2 i a,1 s,1 h m k l
135
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d m,1 g j,2 c g,1 f i,4 j c,2 i a,1 s,1 h m k l
136
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g j,2 c g,1 f i,4 j c,2 i a,1 s,1 h m k l
137
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c g,1 f i,4 j c,2 i a,1 s,1 h m k l
138
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f i,4 j c,2 i a,1 s,1 h m k l
139
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f,0 f i,5 j c,2 i a,1 s,1 h m k l
140
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f,1 f i,5 j c,2 i a,1 s,1 h m k l
141
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f i,5 j c,2 i a,1 s,1 h m k l
142
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j c,2 i a,1 s,1 h m k l
143
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j c,3 i a,1 s,1 h m k l
144
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j i a,1 s,1 h m k l
145
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j i a,2 s,1 h m k l
146
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j i s,1 h m k l
147
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j i d,1 s,2 h m k l
148
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j i d,2 s,2 h m k l
149
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j e,0 i d,3 s,2 h m k l
150
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j e,1 i d,3 s,2 h m k l
151
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j i d,3 s,2 h m k l
152
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j i s,2 h m k l
153
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j i s,3 h m k l
154
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j i b,0 s,4 h m k l
155
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j i b,1 s,4 h m k l
156
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j i b,2 s,4 h m k l
157
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j i b,3 s,4 h m k l
158
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j i s,4 h m k l
159
Found Not Handled Stack <node,# edges>
DFS Found Not Handled Stack <node,# edges> s b a e d g c f j i h m k done l
161
Recursive Depth First Search Get help from friends
163
Linear Order underwear socks pants shoes underwear pants socks shoes
socks underwear pants shoes
164
Linear Order underwear socks pants shoes ?
165
Linear Order a b h c i d j e k g f l ….. l
<preCond>: A Directed Acyclic Graph (DAG) <postCond>: Find one valid linear order a b h c i d j e k Algorithm: Find a sink. Put it last in order. Delete & Repeat g ? f l ….. l
166
Linear Order a b h c i d j e k g f l ….. l
<preCond>: A Directed Acyclic Graph (DAG) <postCond>: Find one valid linear order a b h c i d j e k Algorithm: Find a sink. Put it last in order. Delete & Repeat g f l (n) (n2) ….. l
167
Found Not Handled Stack
Linear Order Found Not Handled Stack Alg: DFS a b h c i d j e k g f g l e f d ….. f
168
Linear Order Found Not Handled Stack a b h c i d j e k g l g e f l d
Alg: DFS a b h c i d j e k g l g e f l d When take off stack add to backwards order ….. f
169
Linear Order Found Not Handled Stack a b h c i d j e k g g e f l d l,f
Alg: DFS a b h c i d j e k g g l e f d When take off stack add to backwards order l,f
170
Linear Order Found Not Handled Stack a b h c i d j e k g e f l d g,l,f
Alg: DFS a b h c i d j e k g f l e d When take off stack add to backwards order g,l,f
171
Linear Order Found Not Handled Stack a b h c i d j e k g f l d e,g,l,f
Alg: DFS a b h c i d j e k g f l d When take off stack add to backwards order e,g,l,f
172
Linear Order Found Not Handled Stack a b h c i d j e k g f l d,e,g,l,f
Alg: DFS a b h c i d j e k g f l When take off stack add to backwards order d,e,g,l,f
173
Linear Order Found Not Handled Stack a b h c i d j e k g k j f l i
Alg: DFS a b h c i d j e k g k j f l i When take off stack add to backwards order d,e,g,l,f
174
Linear Order Found Not Handled Stack a b h c i d j e k g j f l i
Alg: DFS a b h c i d j e k g j f l i When take off stack add to backwards order k,d,e,g,l,f
175
Linear Order Found Not Handled Stack a b h c i d j e k g f l i
Alg: DFS a b h c i d j e k g f l i When take off stack add to backwards order j,k,d,e,g,l,f
176
Linear Order Found Not Handled Stack a b h c i d j e k g f l
Alg: DFS a b h c i d j e k g f l When take off stack add to backwards order i,j,k,d,e,g,l,f
177
Linear Order Found Not Handled Stack a b h c i d j e k g c f l b
Alg: DFS a b h c i d j e k g c f l b When take off stack add to backwards order i,j,k,d,e,g,l,f
178
Linear Order Found Not Handled Stack a b h c i d j e k g f l b
Alg: DFS a b h c i d j e k g f l b When take off stack add to backwards order c,i,j,k,d,e,g,l,f
179
Linear Order Found Not Handled Stack a b h c i d j e k g f l
Alg: DFS a b h c i d j e k g f l When take off stack add to backwards order b,c,i,j,k,d,e,g,l,f
180
Linear Order Found Not Handled Stack a b h c i d j e k g h f l a
Alg: DFS a b h c i d j e k g h f l a When take off stack add to backwards order b,c,i,j,k,d,e,g,l,f
181
Linear Order Found Not Handled Stack a b h c i d j e k g f l a
Alg: DFS a b h c i d j e k g f l a When take off stack add to backwards order h,b,c,i,j,k,d,e,g,l,f
182
Linear Order Found Not Handled Stack a b h c i d j e k g f l
Alg: DFS a b h c i d j e k g f l When take off stack add to backwards order a,h,b,c,i,j,k,d,e,g,l,f done
183
Found Not Handled Stack
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. Consider each edge v … u … u v u… v…
184
Found Not Handled Stack
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. Consider each edge u … v … u v u… v…
185
Found Not Handled Stack
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 Consider each edge u … The nodes in the stack form a path starting at s. v … u v u… v… done
186
Optimization Problems
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 <preCond>: The input is one instance. <postCond>: A valid solution with optimal cost. (minimum or maximum)
187
End
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.