1 Graph ADT and Algorithmic Paradigms Jeff Edmonds York University COSC 2011 Lecture 8 Graph ADT...

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

If you can't read please download the document

Transcript of 1 Graph ADT and Algorithmic Paradigms Jeff Edmonds York University COSC 2011 Lecture 8 Graph ADT...

  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 10
  • Graph ADT: part 1 Andy Mirzaian 10 Last Update: Dec 4, 2014
  • Slide 11
  • Graph ADT: part 2 Andy Mirzaian 11 Last Update: Dec 4, 2014
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 21
  • 21 Graph Search
  • Slide 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
  • Slide 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.
  • Slide 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?
  • Slide 25
  • 25 s a c h k f i l m j e b g d Graph Search
  • Slide 26
  • 26 Graph Search s a c h k f i l m j e b g d
  • Slide 27
  • 27 Graph Search s a c h k f i l m j e b g d
  • Slide 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
  • Slide 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 Dont re-find a node. s a c h k f i l m j e b g d Handle some foundNotHandled node
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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 cant 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
  • Slide 34
  • 34
  • Slide 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
  • Slide 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. :
  • Slide 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.
  • Slide 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
  • Slide 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.
  • Slide 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
  • Slide 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.
  • Slide 42
  • 42 BFS s a c h k f i l m j e b g d Found Not Handled Queue
  • Slide 43
  • 43 BFS s a c h k f i l m j e b g d Found Not Handled Queue s d=0
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 62
  • 62 BFS So far, the nodes have been found in order of length from s.
  • Slide 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.
  • Slide 64
  • 64 BFS Data structure for storing a path to each node: For each node v, store (v) to be parent of v.
  • Slide 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.
  • Slide 66
  • 66
  • Slide 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
  • Slide 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.
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 73
  • 73 Dijkstra's Shortest-Weighted Paths Unhandled edges are unguarded, so we dont want to travel along them, even if they make a shorter path. s
  • Slide 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( )=
  • Slide 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.
  • Slide 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
  • Slide 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.
  • Slide 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 neighbors d values. d( )=20.
  • Slide 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.
  • Slide 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.
  • Slide 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!
  • Slide 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.
  • Slide 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!
  • Slide 84
  • 84
  • Slide 85
  • 85 Dijkstra's u v s Handled nodesFound nodes Handled Edges? Definition of handled paths and d(v)
  • Slide 86
  • 86 Dijkstra's u v s Handled nodesFound nodes Handled Edges Handled Paths? Definition of handled paths and d(v)
  • Slide 87
  • 87 Dijkstra's u v s Handled nodesFound nodes Definition of handled paths and d(v) Handled Edges Handled Paths?
  • Slide 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)
  • Slide 89
  • 89 u The shortest of handled paths to u has length d(u) Dijkstra's & there is an edge from u to v ww 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:
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 102
  • 102 Dijkstra's 0 13 16 17 20 Handle c 19 (d) =c (e) =c
  • Slide 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
  • Slide 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
  • Slide 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)
  • Slide 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
  • Slide 107
  • 107 Dijkstra's
  • Slide 108
  • 108 Dijkstra's
  • Slide 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
  • Slide 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.
  • Slide 111
  • 111 DFS s a c h k f i l m j e b g d Found Not Handled Stack
  • Slide 112
  • 112 DFS s a c h k f i l m j e b g d Found Not Handled Stack s
  • Slide 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
  • Slide 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
  • Slide 115
  • 115 DFS
  • Slide 116
  • 116 DFS The nodes in the stack form a path starting at s.
  • Slide 117
  • 117 DFS s a c h k f i l m j e b g d Found Not Handled Stack
  • Slide 118
  • 118 DFS s a c h k f i l m j e b g d Found Not Handled Stack s,0
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 146
  • 146 DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 152
  • 152 DFS s a c h k f i l m j e b g d s,2 Found Not Handled Stack
  • Slide 153
  • 153 DFS s a c h k f i l m j e b g d s,3 Found Not Handled Stack
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 158
  • 158 DFS s a c h k f i l m j e b g d s,4 Found Not Handled Stack
  • Slide 159
  • 159 DFS s a c h k f i l m j e b g d Found Not Handled Stack done
  • Slide 160
  • 160
  • Slide 161
  • 161 Recursive Depth First Search Get help from friends
  • Slide 162
  • 162
  • Slide 163
  • 163 Linear Order underwear pants socks shoes underwear pants socks shoes socks underwear pants shoes
  • Slide 164
  • 164 Linear Order underwear pants socks shoes ?
  • Slide 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 ?
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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 vu Consider each edge v u
  • Slide 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 vu Consider each edge u v
  • Slide 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 uv Consider each edge u v The nodes in the stack form a path starting at s. done
  • Slide 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
  • Slide 187
  • 187 End