Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
Greed is good. (Some of the time)
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Topological Sort Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological.
3.3 Spanning Trees Tucker, Applied Combinatorics, Section 3.3, by Patti Bodkin and Tamsen Hunter.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Data Structure and Algorithms (BCS 1223) GRAPH. Introduction of Graph A graph G consists of two things: 1.A set V of elements called nodes(or points or.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 3 The Greedy Method 3.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
Midterm 2 Overview Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
Graphs & Graph Algorithms 2
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Graphs & Graph Algorithms 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
WEIGHTED GRAPHS. Weighted Graphs zGraph G = (V,E) such that there are weights/costs associated with each edge Õw((a,b)): cost of edge (a,b) Õrepresentation:
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 2 Graph Algorithms.
Minimum Spanning Trees
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
COSC 2007 Data Structures II Chapter 14 Graphs III.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 5 Graph Algorithms.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
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:
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
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.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Minimum Spanning Trees and Shortest Paths
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Minimum Spanning Tree.
Minimum Spanning Tree.
Graphs & Graph Algorithms 2
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Minimum Spanning Trees
Chapter 11 Graphs.
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Algorithm Course Dr. Aref Rashad
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 .
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park

Overview Shortest path Djikstra’s algorithm Spanning trees Minimum spanning tree Kruskal’s algorithm Graph implementation Adjacency list / matrix

Single Source Shortest Path Common graph problem Find path from X to Y with lowest edge weight Find path from X to any Y with lowest edge weight Useful for many applications Shortest route in map Lowest cost trip Most efficient internet route Can solve both problems with same algorithm would actually do something slightly different if we only wanted shortest path from X to Y don’t need to find shortest path from College Park to Seattle in order to find shortest path from College Park to Miami

Shortest Path – Djikstra’s Algorithm Maintain Nodes with known shortest path from start  S Cost of shortest path to node K from start  C[K] Only for paths through nodes in S Predecessor to K on shortest path  P[K] Updated whenever new (lower) C[K] discovered Remembers actual path with lowest cost

Shortest Path – Intuition for Djikstra’s At each step in the algorithm Shortest paths are known for nodes in S Store in C[K] length of shortest path to node K (for all paths through nodes in { S } ) Add to { S } next closest node S

Shortest Path – Intuition for Djikstra’s Update distance to J after adding node K Previous shortest paths already in C[ K ] Possibly shorter path by going through node K Compare C[ J ] with C[ K ] + weight of (K,J)

Shortest Path – Djikstra’s Algorithm Algorithm Add starting node to S Repeat until all nodes in S Find node K not in S with smallest C[ K ] Add K to S Examine C[J] for all neighbors J of K not in S If ( C[K] + weight for edge (K,J) ) < C[J] New shortest path by first going to K, then J Update C[J]  C[K] + weight for edge (K,J) Update P[J]  K

Shortest Path – Djikstra’s Algorithm S = {start}, P[ ] = none for all nodes C[start] = 0, C[ ] =  for all other nodes while ( not all nodes in S ) find node K not in S with smallest C[K] add K to S for each node J not in S adjacent to K if ( C[K] + cost of (K,J) < C[J] ) C[J] = C[K] + cost of (K,J) P[J] = K Optimal solution computed with greedy algorithm

Djikstra’s Shortest Path Example Initial state S =  CP 10 none 2  3  4  5 

Djikstra’s Shortest Path Example Find shortest paths starting from node 1 S = 1 CP 10 none 2  3  4  5 

Djikstra’s Shortest Path Example Update C[K] for all neighbors of 1 not in { S } S = { 1 } C[2] = min ( , C[1] + (1,2) ) = min ( , 0 + 5) = 5 C[3] = min ( , C[1] + (1,3) ) = min ( , 0 + 8) = 8 CP 10 none  5 

Djikstra’s Shortest Path Example Find node K with smallest C[K] and add to S S = { 1, 2 } CP 10 none  5 

Djikstra’s Shortest Path Example Update C[K] for all neighbors of 2 not in S S = { 1, 2 } C[3] = min (8, C[2] + (2,3) ) = min (8, 5 + 1) = 6 C[4] = min ( , C[2] + (2,4) ) = min ( , ) = 15 CP 10 none  none

Djikstra’s Shortest Path Example Find node K with smallest C[K] and add to S S = { 1, 2, 3 } CP 10 none  none

Djikstra’s Shortest Path Example Update C[K] for all neighbors of 3 not in S { S } = 1, 2, 3 C[4] = min (15, C[3] + (3,4) ) = min (15, 6 + 3) = 9 CP 10 none 

Djikstra’s Shortest Path Example Find node K with smallest C[K] and add to S { S } = 1, 2, 3, 4 CP 10 none 

Djikstra’s Shortest Path Example Update C[K] for all neighbors of 4 not in S S = { 1, 2, 3, 4 } C[5] = min ( , C[4] + (4,5) ) = min ( , 9 + 9) = 18 CP 10 none

Djikstra’s Shortest Path Example Find node K with smallest C[K] and add to S S = { 1, 2, 3, 4, 5 } CP 10 none

Djikstra’s Shortest Path Example All nodes in S, algorithm is finished S = { 1, 2, 3, 4, 5 } CP 10 none

Djikstra’s Shortest Path Example Find shortest path from start to K Start at K Trace back predecessors in P[ ] Example paths (in reverse) 2  1 3  2  1 4  3  2  1 5  4  3  2  1 CP 10 none

Algorithm animations Good animation of Dijkstra’s algorithm at u.ac.jp/~ikeda/suuri/dijkstra/Dijkstra.shtml Also has animations of Prim’s and Kruskal’s algorithms for minimal spanning trees

Spanning Tree Set of edges connecting all nodes in graph need N-1 edges for N nodes no cycles, can be thought of as a tree Can build tree during traversal

Spanning Tree Construction for all nodes X set X.tag = False set X.parent = Null { Discovered } = { 1st node } while ( { Discovered }   ) take node X out of { Discovered } if (X.tag = False) set X.tag = True for each successor Y of X if (Y.tag = False) set Y.parent = X // add (X,Y) to tree add Y to { Discovered }

Breadth & Depth First Spanning Trees Breadth-firstDepth-first

Depth-First Spanning Tree Example

Breadth-First Spanning Tree Example

Spanning Tree Construction Multiple spanning trees possible Different breadth-first traversals Nodes same distance visited in different order Different depth-first traversals Neighbors of node visited in different order Different traversals yield different spanning trees

Minimum Spanning Tree (MST) Spanning tree with minimum total edge weight Multiple MSTs possible (with same weight)

MST – Kruskal’s Algorithm sort edges by weight (from least to most) tree =  for each edge (X,Y) in order if it does not create a cycle add (X,Y) to tree stop when tree has N–1 edges Optimal solution computed with greedy algorithm

MST – Kruskal’s Algorithm Example

MST – Kruskal’s Algorithm When does adding (X,Y) to tree create cycle? Traversal approach Traverse tree starting at X If we can reach Y, adding (X,Y) would create cycle Connected subgraph approach Maintain set of nodes for each connected subgraph Initialize one connected subgraph for each node If X, Y in same set, adding (X,Y) would create cycle Otherwise We can add edge (X,Y) to spanning tree Merge sets containing X, Y (single subgraph)

MST – Connected Subgraph Example

Graph Implementation Representations Explicit edges (a,b) Maintain set of edges for every node Adjacency matrix 2D array of neighbors Adjacency list Linked list of neighbors Important for very large graphs Affects efficiency / storage

Adjacency Matrix Representation 2D array Position j, k  edge between nodes n j, n k Unweighted graph Matrix elements  boolean Weighted graph Matrix elements  weight

Adjacency Matrix Example

Adjacency Matrix Properties Single array for entire graph Only upper / lower triangle matrix needed for undirected graph Since n j, n k implies n k, n j

Adjacency List Representation Linked list for each node Unweighted graph store neighbor Weighted graph store neighbor, weight

Adjacency List Example Unweighted graph Weighted graph

Graph Space Requirements Adjacency matrix ½ N 2 entries (for graph with N nodes, E edges) Many empty entries for large graphs Can implement as sparse array Adjacency list E edges Each edge stores reference to node & next edge Explicit edges E edges Each edge stores reference to 2 nodes

Graph Time Requirements Complexity of operations For graph with N nodes, E edges OperationAdj MatrixAdj List Find edgeO(1)O(E/N) Insert nodeO(1)O(E/N) Insert edgeO(1)O(E/N) Delete nodeO(N)O(E) Delete edgeO(1)O(E/N)