Data Structures Graphs. Graph… ADT? Not quite an ADT… operations not clear A formalism for representing relationships between objects Graph G = (V,E)

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
1 CSE 326: Data Structures: Graphs Lecture 19: Monday, Feb 24, 2003.
CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007.
1 Shortest Path Algorithms Given a graph G = (V, E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. weighted.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
CSE332: Data Abstractions Lecture 17: Shortest Paths Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 15: Introduction to Graphs Dan Grossman Spring 2010.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
CSE 326: Data Structures Lecture #19 Graphs I Alon Halevy Spring Quarter 2001.
CSE 326 Graphs David Kaplan Dept of Computer Science & Engineering Autumn 2001.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
1 CSE 326: Data Structures: Graphs Lecture 19: Monday, Feb 24, 2003.
CSE 326: Data Structures Graphs Ben Lerner Summer 2007.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CSE 326: Data Structures Lecture #17 Trees and DAGs and Graphs, Oh MY! Bart Niswonger Summer Quarter 2001.
CSE 326: Data Structures Spanning Trees Ben Lerner Summer 2007.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
CSE 326: Data Structures Graphs Ben Lerner Summer 2007.
CSE 589 Applied Algorithms Course Introduction. CSE Lecture 1 - Spring Instructors Instructor –Richard Ladner –206.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
CSE 221: Algorithms and Data Structures Lecture #9 Graphs (with no Axes to Grind) Steve Wolfman 2010W2 1.
CS 146: Data Structures and Algorithms July 21 Class Meeting
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Chapter 2 Graph Algorithms.
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
CSE332: Data Structures & Algorithms Lecture 13: Introduction to Graphs Dan Grossman Fall 2013.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
CSE 326: Data Structures First Post-Schism Lecture Lecture #22 SteganoGRAPHy Steve Wolfman Winter Quarter 2000.
CPSC 320: Intermediate Algorithm Design & Analysis Greedy Algorithms and Graphs Steve Wolfman 1.
CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
CSE373: Data Structures & Algorithms Lecture 15: Introduction to Graphs Nicki Dell Spring 2014.
CSE332: Data Abstractions Lecture 15: Introduction to Graphs Tyler Robison 2010 Summer 1.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
CSE 326: Data Structures Final Exam Review James Fogarty Autumn 2007 Lecture n - 1.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
CSE373: Data Structures & Algorithms Lecture 14: Topological Sort / Graph Traversals Dan Grossman Fall 2013.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
1 CSE 332: Graphs Richard Anderson Spring Announcements This week and next week – Graph Algorithms Reading, Monday and Wednesday, Weiss
CSE 326: Data Structures Lecture #23 Dijkstra and Kruskal (sittin’ in a graph) Steve Wolfman Winter Quarter 2000.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
Instructor: Lilian de Greef Quarter: Summer 2017
May 12th – Minimum Spanning Trees
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
CSE373: Data Structures & Algorithms Lecture 13: Topological Sort / Graph Traversals Kevin Quinn Fall 2015.
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Winter 2015.
CSE373: Data Structures & Algorithms Lecture 15: Introduction to Graphs Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Spring 2016.
Paul Beame in lieu of Richard Anderson
CSE 373 Data Structures and Algorithms
CSE373: Data Structures & Algorithms Lecture 18: Dijkstra’s Algorithm
Based on slides of Dan Suciu
Directed Graph Algorithms
Directed Graph Algorithms
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Fundamental Structures of Computer Science II
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Richard Anderson Spring 2016
CSE 332: Minimum Spanning Trees
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Data Structures Graphs

Graph… ADT? Not quite an ADT… operations not clear A formalism for representing relationships between objects Graph G = (V,E) –Set of vertices: V = {v 1,v 2,…,v n } –Set of edges: E = {e 1,e 2,…,e m } where each e i connects two vertices (v i1,v i2 ) 2 Han Leia Luke V = {Han, Leia, Luke} E = {(Luke, Leia), (Han, Leia), (Leia, Han)}

Examples of Graphs The web –Vertices are webpages –Each edge is a link from one page to another Call graph of a program –Vertices are subroutines –Edges are calls and returns Social networks –Vertices are people –Edges connect friends 3

Graph Definitions In directed graphs, edges have a direction: In undirected graphs, they don’t (are two-way): v is adjacent to u if (u,v)  E 4 Han Leia Luke Han Leia Luke

Weighted Graphs Mukilteo Edmonds Seattle Bremerton Bainbridge Kingston Clinton Each edge has an associated weight or cost.

Paths and Cycles A path is a list of vertices {v 1, v 2, …, v n } such that (v i, v i+1 )  E for all 0  i < n. A cycle is a path that begins and ends at the same node. 6 Seattle San Francisco Dallas Chicago Salt Lake City p = {Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle}

Path Length and Cost Path length: the number of edges in the path Path cost: the sum of the costs of each edge 7 Seattle San Francisco Dallas Chicago Salt Lake City length(p) = 5cost(p) = 11.5

More Definitions: Simple Paths and Cycles A simple path repeats no vertices (except that the first can also be the last): p = {Seattle, Salt Lake City, San Francisco, Dallas} p = {Seattle, Salt Lake City, Dallas, San Francisco, Seattle} A cycle is a path that starts and ends at the same node: p = {Seattle, Salt Lake City, Dallas, San Francisco, Seattle} p = {Seattle, Salt Lake City, Seattle, San Francisco, Seattle} A simple cycle is a cycle that is also a simple path (in undirected graphs, no edge can be repeated) 8

Trees as Graphs Every tree is a graph with some restrictions: –the tree is directed –there are no cycles (directed or undirected) –there is a directed path from the root to every node 9 A B DE C F HG

Directed Acyclic Graphs (DAGs) DAGs are directed graphs with no (directed) cycles. 10 main() add() access() mult() read() Aside: If program call- graph is a DAG, then all procedure calls can be in- lined

Rep 1: Adjacency Matrix A |V| x |V| array in which an element (u,v) is true if and only if there is an edge from u to v 11 Han Leia Luke HanLukeLeia Han Luke Leia Space requirements? Runtimes: Iterate over vertices? Iterate over edges? Iterate edges adj. to vertex? Existence of edge?

Rep 2: Adjacency List A |V| -ary list (array) in which each entry stores a list (linked list) of all adjacent vertices 12 Han Leia Luke Han Luke Leia Space requirements? Runtimes: Iterate over vertices? Iterate over edges? Iterate edges adj. to vertex? Existence of edge?

Some Applications: Moving Around Washington 13 What’s the shortest way to get from Seattle to Pullman? Edge labels:

Some Applications: Moving Around Washington 14 What’s the fastest way to get from Seattle to Pullman? Edge labels:

Some Applications: Reliability of Communication 15 If Wenatchee’s phone exchange goes down, can Seattle still talk to Pullman?

Some Applications: Bus Routes in Downtown Seattle 16 If we’re at 3 rd and Pine, how can we get to 1 st and University using Metro? How about 4 th and Seneca?

Application: Topological Sort Given a directed graph, G = (V,E), output all the vertices in V such that no vertex is output before any other vertex with an edge to it. 17 CSE 142CSE 143 CSE 321 CSE 341 CSE 378 CSE 326 CSE 370 CSE 403 CSE 421 CSE 467 CSE 451 CSE 322 Is the output unique?

Topological Sort: Take One 1.Label each vertex with its in-degree (# of inbound edges) 2.While there are vertices remaining: a.Choose a vertex v of in-degree zero ; output v b.Reduce the in-degree of all vertices adjacent to v c.Remove v from the list of vertices 18 Runtime:

void Graph::topsort(){ Vertex v, w; labelEachVertexWithItsIn-degree(); for (int counter=0; counter < NUM_VERTICES; counter++){ v = findNewVertexOfDegreeZero(); v.topologicalNum = counter; for each w adjacent to v w.indegree--; } 19

Topological Sort: Take Two 1.Label each vertex with its in-degree 2.Initialize a queue Q to contain all in-degree zero vertices 3.While Q not empty a.v = Q.dequeue; output v b.Reduce the in-degree of all vertices adjacent to v c.If new in-degree of any such vertex u is zero Q.enqueue(u) 20 Runtime: Note: could use a stack, list, set, box, … instead of a queue

void Graph::topsort(){ Queue q(NUM_VERTICES); int counter = 0; Vertex v, w; labelEachVertexWithItsIn-degree(); q.makeEmpty(); for each vertex v if (v.indegree == 0) q.enqueue(v); while (!q.isEmpty()){ v = q.dequeue(); v.topologicalNum = ++counter; for each w adjacent to v if (--w.indegree == 0) q.enqueue(w); } 21 intialize the queue get a vertex with indegree 0 insert new eligible vertices Runtime:

Graph Connectivity Undirected graphs are connected if there is a path between any two vertices Directed graphs are strongly connected if there is a path from any one vertex to any other Directed graphs are weakly connected if there is a path between any two vertices, ignoring direction A complete graph has an edge between every pair of vertices 22

Graph Traversals Breadth-first search (and depth-first search) work for arbitrary (directed or undirected) graphs - not just mazes! –Must mark visited vertices so you do not go into an infinite loop! Either can be used to determine connectivity: –Is there a path between two given vertices? –Is the graph (weakly) connected? Which one: –Uses a queue? –Uses a stack? –Always finds the shortest path (for unweighted graphs)? 23

CSE 326: Data Structures Graph Traversals James Fogarty Autumn 2007

Graph Connectivity Undirected graphs are connected if there is a path between any two vertices Directed graphs are strongly connected if there is a path from any one vertex to any other Directed graphs are weakly connected if there is a path between any two vertices, ignoring direction A complete graph has an edge between every pair of vertices 25

Graph Traversals Breadth-first search (and depth-first search) work for arbitrary (directed or undirected) graphs - not just mazes! –Must mark visited vertices. Why? –So you do not go into an infinite loop! It’s not a tree. Either can be used to determine connectivity: –Is there a path between two given vertices? –Is the graph (weakly/strongly) connected? Which one: –Uses a queue? –Uses a stack? –Always finds the shortest path (for unweighted graphs)? 26

The Shortest Path Problem Given a graph G, edge costs c i,j, and vertices s and t in G, find the shortest path from s to t. For a path p = v 0 v 1 v 2 … v k –unweighted length of path p = k (a.k.a. length) –weighted length of path p =  i=0..k-1 c i,i+1 (a.k.a cost) –Path length equals path cost when ? 27

Single Source Shortest Paths (SSSP) Given a graph G, edge costs c i,j, and vertex s, find the shortest paths from s to all vertices in G. –Is this harder or easier than the previous problem? 28

All Pairs Shortest Paths (APSP) Given a graph G and edge costs c i,j, find the shortest paths between all pairs of vertices in G. –Is this harder or easier than SSSP? –Could we use SSSP as a subroutine to solve this? 29

Depth-First Graph Search DFS( Start, Goal_test) push(Start, Open); repeat if (empty(Open)) then return fail; Node := pop(Open); if (Goal_test(Node)) then return Node; for each Child of node do if (Child not already visited) then push(Child, Open); Mark Node as visited; end 30 Open – Stack Criteria – Pop

Breadth-First Graph Search BFS( Start, Goal_test) enqueue(Start, Open); repeat if (empty(Open)) then return fail; Node := dequeue(Open); if (Goal_test(Node)) then return Node; for each Child of node do if (Child not already visited) then enqueue(Child, Open); Mark Node as visited; end 31 Open – Queue Criteria – Dequeue (FIFO)

Comparison: DFS versus BFS Depth-first search –Does not always find shortest paths –Must be careful to mark visited vertices, or you could go into an infinite loop if there is a cycle Breadth-first search –Always finds shortest paths – optimal solutions –Marking visited nodes can improve efficiency, but even without doing so search is guaranteed to terminate –Is BFS always preferable? 32

DFS Space Requirements Assume: –Longest path in graph is length d –Highest number of out-edges is k DFS stack grows at most to size dk –For k=10, d=15, size is

BFS Space Requirements Assume –Distance from start to a goal is d –Highest number of out edges is k BFS Queue could grow to size k d –For k=10, d=15, size is 1,000,000,000,000,000 34

Conclusion For large graphs, DFS is hugely more memory efficient, if we can limit the maximum path length to some fixed d. –If we knew the distance from the start to the goal in advance, we can just not add any children to stack after level d –But what if we don’t know d in advance? 35

Iterative-Deepening DFS (I) Bounded_DFS(Start, Goal_test, Limit) Start.dist = 0; push(Start, Open); repeat if (empty(Open)) then return fail; Node := pop(Open); if (Goal_test(Node)) then return Node; if (Node.dist  Limit) then return fail; for each Child of node do if (Child not already i-visited) then Child.dist := Node.dist + 1; push(Child, Open); Mark Node as i-visited; end 36

Iterative-Deepening DFS (II) IDFS_Search(Start, Goal_test) i := 1; repeat answer := Bounded_DFS(Start, Goal_test, i); if (answer != fail) then return answer; i := i+1; end 37

Analysis of IDFS Work performed with limit < actual distance to G is wasted – but the wasted work is usually small compared to amount of work done during the last iteration 38 Ignore low order terms! Same time complexity as BFS Same space complexity as (bounded) DFS

Saving the Path Our pseudocode returns the goal node found, but not the path to it How can we remember the path? –Add a field to each node, that points to the previous node along the path –Follow pointers from goal back to start to recover path 39

Example 40 Seattle San Francisco Dallas Salt Lake City

Example (Unweighted Graph) 41 Seattle San Francisco Dallas Salt Lake City

Example (Unweighted Graph) 42 Seattle San Francisco Dallas Salt Lake City

Graph Search, Saving Path Search( Start, Goal_test, Criteria) insert(Start, Open); repeat if (empty(Open)) then return fail; select Node from Open using Criteria; if (Goal_test(Node)) then return Node; for each Child of node do if (Child not already visited) then Child.previous := Node; Insert( Child, Open ); Mark Node as visited; end 43

Weighted SSSP: The Quest For Food 44 Vending Machine in EE1 ALLEN HUB Delfino’s Ben & Jerry’s Neelam’s Cedars Coke Closet Home Schultzy’s Parent’s Home Café Allegro 10 The Ave U Village , Can we calculate shortest distance to all nodes from Allen Center?

Weighted SSSP: The Quest For Food 45 Vending Machine in EE1 ALLEN HUB Delfino’s Ben & Jerry’s Neelam’s Cedars Coke Closet Home Schultzy’s Parent’s Home Café Allegro 10 The Ave U Village , Can we calculate shortest distance to all nodes from Allen Center?

Edsger Wybe Dijkstra ( ) Invented concepts of structured programming, synchronization, weakest precondition, and "semaphores" for controlling computer processes. The Oxford English Dictionary cites his use of the words "vector" and "stack" in a computing context. Believed programming should be taught without computers 1972 Turing Award “In their capacity as a tool, computers will be but a ripple on the surface of our culture. In their capacity as intellectual challenge, they are without precedent in the cultural history of mankind.” 46

General Graph Search Algorithm Search( Start, Goal_test, Criteria) insert(Start, Open); repeat if (empty(Open)) then return fail; select Node from Open using Criteria; if (Goal_test(Node)) then return Node; for each Child of node do if (Child not already visited) then Insert( Child, Open ); Mark Node as visited; end 47 Open – some data structure (e.g., stack, queue, heap) Criteria – some method for removing an element from Open

Shortest Path for Weighted Graphs Given a graph G = (V, E) with edge costs c(e), and a vertex s  V, find the shortest (lowest cost) path from s to every vertex in V Assume: only positive edge costs 48

Dijkstra’s Algorithm for Single Source Shortest Path Similar to breadth-first search, but uses a heap instead of a queue: –Always select (expand) the vertex that has a lowest-cost path to the start vertex Correctly handles the case where the lowest-cost (shortest) path to a vertex is not the one with fewest edges 49

CSE 326: Data Structures Dijkstra’s Algorithm

Dijkstra, Edsger Wybe Legendary figure in computer science; was a professor at University of Texas. Supported teaching introductory computer courses without computers (pencil and paper programming) Supposedly wouldn’t (until very late in life) read his ; so, his staff had to print out messages and put them in his box. 51 E.W. Dijkstra ( ) 1972 Turning Award Winner, Programming Languages, semaphores, and …

Dijkstra’s Algorithm: Idea Adapt BFS to handle weighted graphs Two kinds of vertices: –Finished or known vertices Shortest distance has been computed –Unknown vertices Have tentative distance 52

Dijkstra’s Algorithm: Idea At each step: 1)Pick closest unknown vertex 2)Add it to known vertices 3)Update distances 53

Dijkstra’s Algorithm: Pseudocode Initialize the cost of each node to  Initialize the cost of the source to 0 While there are unknown nodes left in the graph Select an unknown node b with the lowest cost Mark b as known For each node a adjacent to b a’s cost = min(a’s old cost, b’s cost + cost of (b, a)) a’s prev path node = b 54

Important Features Once a vertex is made known, the cost of the shortest path to that node is known While a vertex is still not known, another shorter path to it might still be found The shortest path itself can found by following the backward pointers stored in node.path 55

Dijkstra’s Algorithm in action 56 A B D C FH E G 0       VertexVisited?CostFound by A0 B?? C D E F G H

Dijkstra’s Algorithm in action 57 A B D C FH E G 0 2  4 1   VertexVisited?CostFound by AY0 B<=2A C<=1A D<=4A E?? F G H

Dijkstra’s Algorithm in action 58 A B D C FH E G 0 2   VertexVisited?CostFound by AY0 B<=2A CY1A D<=4A E<=12C F?? G H

Dijkstra’s Algorithm in action 59 A B D C FH E G   VertexVisited?CostFound by AY0 BY2A CY1A D<=4A E<=12C F<=4B G?? H

Dijkstra’s Algorithm in action 60 A B D C FH E G   VertexVisited?CostFound by AY0 BY2A CY1A DY4A E<=12C F<=4B G?? H

Dijkstra’s Algorithm in action 61 A B D C FH E G  VertexVisited?CostFound by AY0 BY2A CY1A DY4A E<=12C FY4B G?? H<=7F

Dijkstra’s Algorithm in action 62 A B D C FH E G VertexVisited?CostFound by AY0 BY2A CY1A DY4A E<=12C FY4B G<=8H HY7F

Dijkstra’s Algorithm in action 63 A B D C FH E G VertexVisited?CostFound by AY0 BY2A CY1A DY4A E<=11G FY4B GY8H HY7F

Dijkstra’s Algorithm in action 64 A B D C FH E G VertexVisited?CostFound by AY0 BY2A CY1A DY4A EY11G FY4B GY8H HY7F

Your turn VVisited?CostFound by v0 v1 v2 v3 v4 v5 v6 65 v3v3 v6v6 v1v1 v2v2 v4v4 v5v5 v0v0 s

Answer VVisited?CostFound by v0Y0 V1Y6V3 V2Y2V0 V3Y1V0 V4Y2V3 V5Y4V2 V6Y6V3 66 v3v3 v6v6 v1v1 v2v2 v4v4 v5v5 v0v0 s

Dijkstra’s Alg: Implementation Initialize the cost of each node to  Initialize the cost of the source to 0 While there are unknown nodes left in the graph Select the unknown node b with the lowest cost Mark b as known For each node a adjacent to b a’s cost = min(a’s old cost, b’s cost + cost of (b, a)) a’s prev path node = b (if we updated a’s cost) 67 What data structures should we use? Running time?

void Graph::dijkstra(Vertex s){ Vertex v,w; Initialize s.dist = 0 and set dist of all other vertices to infinity while (there exist unknown vertices, find the one b with the smallest distance) b.known = true; for each a adjacent to b if (!a.known) if (b.dist + weight(b,a) < a.dist){ a.dist = (b.dist + weight(b,a)); a.path = b; } 68 Sounds like deleteMin on a heap… Sounds like adjacency lists Sounds like decreaseKey Running time: O(|E| log |V|) – there are |E| edges to examine, and each one causes a heap operation of time O(log |V|)

Dijkstra’s Algorithm: Summary Classic algorithm for solving SSSP in weighted graphs without negative weights A greedy algorithm (irrevocably makes decisions without considering future consequences) Intuition for correctness: –shortest path from source vertex to itself is 0 –cost of going to adjacent nodes is at most edge weights –cheapest of these must be shortest path to that node –update paths for new node and continue picking cheapest path 69

Correctness: The Cloud Proof How does Dijkstra’s decide which vertex to add to the Known set next? If path to V is shortest, path to W must be at least as long (or else we would have picked W as the next vertex) So the path through W to V cannot be any shorter! 70 The Known Cloud V Next shortest path from inside the known cloud W Better path to V? No! Source

Correctness: Inside the Cloud Prove by induction on # of nodes in the cloud: Initial cloud is just the source with shortest path 0 Assume: Everything inside the cloud has the correct shortest path Inductive step: Only when we prove the shortest path to some node v (which is not in the cloud) is correct, we add it to the cloud 71 When does Dijkstra’s algorithm not work?

The Trouble with Negative Weight Cycles 72 AB CD E What’s the shortest path from A to E? Problem?

Dijkstra’s vs BFS At each step: 1)Pick closest unknown vertex 2)Add it to finished vertices 3)Update distances Dijkstra’s Algorithm At each step: 1)Pick vertex from queue 2)Add it to visited vertices 3)Update queue with neighbors Breadth-first Search 73 Some Similarities :

Single-Source Shortest Path Given a graph G = (V, E) and a single distinguished vertex s, find the shortest weighted path from s to every other vertex in G. All-Pairs Shortest Path: Find the shortest paths between all pairs of vertices in the graph. How? 74

Analysis Total running time for Dijkstra’s: O(|V| log |V| + |E| log |V|) (heaps) What if we want to find the shortest path from each point to ALL other points? 75

Dynamic Programming Algorithmic technique that systematically records the answers to sub-problems in a table and re-uses those recorded results (rather than re-computing them). Simple Example: Calculating the Nth Fibonacci number. Fib(N) = Fib(N-1) + Fib(N-2) 76

Floyd-Warshall for (int k = 1; k =< V; k++) for (int i = 1; i =< V; i++) for (int j = 1; j =< V; j++) if ( ( M[i][k]+ M[k][j] ) < M[i][j] ) M[i][j] = M[i][k]+ M[k][j] 77 Invariant: After the kth iteration, the matrix includes the shortest paths for all pairs of vertices (i,j) containing only vertices 1..k as intermediate vertices

abcde a b c--0-1 d---04 e b c d e a Initial state of the matrix: M[i][j] = min(M[i][j], M[i][k]+ M[k][j])

abcde a b-0-21 c--0-1 d---04 e b c d e a Floyd-Warshall - for All-pairs shortest path Final Matrix Contents

CSE 326: Data Structures Spanning Trees

A Hidden Tree 81 Start End

Spanning Tree in a Graph 82 Vertex = router Edge = link between routers Spanning tree - Connects all the vertices - No cycles

Undirected Graph G = (V,E) –V is a set of vertices (or nodes) –E is a set of unordered pairs of vertices V = {1,2,3,4,5,6,7} E = {{1,2},{1,6},{1,5},{2,7},{2,3}, {3,4},{4,7},{4,5},{5,6}} 2 and 3 are adjacent 2 is incident to edge {2,3}

Spanning Tree Problem Input: An undirected graph G = (V,E). G is connected. Output: T contained in E such that –(V,T) is a connected graph –(V,T) has no cycles 84

Spanning Tree Algorithm 85 ST(i: vertex) mark i; for each j adjacent to i do if j is unmarked then Add {i,j} to T; ST(j); end{ST} Main T := empty set; ST(1); end{Main}

Example of Depth First Search ST(1)

Example Step ST(1) {1,2} {2,7} {7,5} {5,4} {4,3} {5,6}

Minimum Spanning Trees Given an undirected graph G=(V,E), find a graph G’=(V, E’) such that: –E’ is a subset of E –|E’| = |V| - 1 –G’ is connected – is minimal Applications: wiring a house, power grids, Internet connections 88 G’ is a minimum spanning tree.

Minimum Spanning Tree Problem Input: Undirected Graph G = (V,E) and a cost function C from E to the reals. C(e) is the cost of edge e. Output: A spanning tree T with minimum total cost. That is: T that minimizes 89

Best Spanning Tree Each edge has the probability that it won’t fail Find the spanning tree that is least likely to fail

Example of a Spanning Tree Probability of success =.85 x.95 x.89 x.95 x 1.0 x.84 =.5735

Minimum Spanning Tree Problem Input: Undirected Graph G = (V,E) and a cost function C from E to the reals. C(e) is the cost of edge e. Output: A spanning tree T with minimum total cost. That is: T that minimizes 92

Reducing Best to Minimum 93 Let P(e) be the probability that an edge doesn’t fail. Define: Minimizing is equivalent to maximizing because

Example of Reduction Best Spanning Tree ProblemMinimum Spanning Tree Problem

Find the MST

Find the MST 96 A C B D F H G E

Two Different Approaches 97 Prim’s Algorithm Looks familiar! Kruskals’s Algorithm Completely different!

Prim’s algorithm Idea: Grow a tree by adding an edge from the “known” vertices to the “unknown” vertices. Pick the edge with the smallest weight. 98 G v known

Prim’s Algorithm for MST A node-based greedy algorithm Builds MST by greedily adding nodes 1.Select a node to be the “root” mark it as known Update cost of all its neighbors 2.While there are unknown nodes left in the graph a.Select an unknown node b with the smallest cost from some known node a b.Mark b as known c.Add (a, b) to MST d.Update cost of all nodes adjacent to b 99

Find MST using Prim’s VKwnDistancepath v1 v2 v3 v4 v5 v6 v7 100 v4v4 v7v7 v2v2 v3v3 v5v5 v6v6 v1v1 Start with V Your Turn Order Declared Known: V1V1

Prim’s Algorithm Analysis Running time: Same as Dijkstra’s:O(|E| log |V|) Correctness: Proof is similar to Dijkstra’s 101

Kruskal’s MST Algorithm Idea: Grow a forest out of edges that do not create a cycle. Pick an edge with the smallest weight. 102 G=(V,E) v

Kruskal’s Algorithm for MST An edge-based greedy algorithm Builds MST by greedily adding edges 1.Initialize with empty MST all vertices marked unconnected all edges unmarked 2.While there are still unmarked edges a.Pick the lowest cost edge (u,v) and mark it b.If u and v are not already connected, add (u,v) to the MST and mark u and v as connected to each other 103 Doesn’t it sound familiar?

Example of Kruskal {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

Data Structures for Kruskal Sorted edge list Disjoint Union / Find –Union(a,b) - union the disjoint sets named by a and b –Find(a) returns the name of the set containing a 105 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

Example of DU/F {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5} Find(5) = 7 Find(4) = 7

Example of DU/F {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5} Find(1) = 1 Find(6) = 7

Example of DU/F {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5} Union(1,7)

Kruskal’s Algorithm with DU / F 109 Sort the edges by increasing cost; Initialize A to be empty; for each edge {i,j} chosen in increasing order do u := Find(i); v := Find(j); if not(u = v) then add {i,j} to A; Union(u,v);

Kruskal code void Graph::kruskal(){ int edgesAccepted = 0; DisjSet s(NUM_VERTICES); while (edgesAccepted < NUM_VERTICES – 1){ e = smallest weight edge not deleted yet; // edge e = (u, v) uset = s.find(u); vset = s.find(v); if (uset != vset){ edgesAccepted++; s.unionSets(uset, vset); } 110 2|E| finds |V| unions |E| heap ops

Find MST using Kruskal’s 111 A C B D FH G E Total Cost: Now find the MST using Prim’s method. Under what conditions will these methods give the same result?