Graphs & Graph Algorithms 2

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.
O(N 1.5 ) divide-and-conquer technique for Minimum Spanning Tree problem Step 1: Divide the graph into  N sub-graph by clustering. Step 2: Solve each.
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.
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.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 3 The Greedy Method 3.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
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.
Chapter 9 Graph algorithms. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
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 & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
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.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
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:
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
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
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.
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.
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.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Graphs Upon completion you will be able to:
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.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Minimum Spanning Trees and Shortest Paths
CS 3343: Analysis of Algorithms
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Graph Operations And Representation
Minimum Spanning Tree.
Minimum Spanning Tree.
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Minimum Spanning Trees
Graph Operations And Representation
Chapter 11 Graphs.
Minimum Spanning Tree Algorithms
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Minimum Spanning Tree.
CSE 373 Data Structures and Algorithms
Graphs.
CSE 373: Data Structures and Algorithms
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Graph Operations And Representation
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Algorithm Course Dr. Aref Rashad
Chapter 9 Graph algorithms
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 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park

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

Spanning Tree Tree connecting all nodes in graph N-1 edges for N nodes 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-first Depth-first

Depth-First Spanning Tree Example

Breadth-First Spanning Tree Example

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 Cycle if reach Y Connected subgraph approach Maintain set of nodes for each connected subgraph Initialize one connected subgraph for each node When edge (X,Y) is added to tree Merge sets containing X, Y Cycle if X,Y in same set

MST – Connected Subgraph Example

MST – Connected Subgraph Example

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

Shortest Path – Djikstra’s Algorithm Maintain Nodes with known shortest path  { S } Cost of shortest path to node  C[…] For paths through nodes in { S } Algorithm Repeat until all nodes in { S } Find node K not in { S } with smallest C[ K ] Add K to { S } Update C[M] for all neighbors M of K not in { S } By checking whether C[M] can be reduced by first going to K, then adding weight for edge (K,M)

Shortest Path – Djikstra’s Algorithm C[X] = 0 C[Y] =  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 M not in { S } adjacent to K C[M] = min ( C[M] , C[K] + cost of (K,M) ) Optimal solution computed with greedy algorithm

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 ] to C[ K ] + weight of (K,J)

Djikstra’s Shortest Path Example Initial state { S } =  C[1] = 0 C[2] =  C[3] =  C[4] =  C[5] = 

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

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

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

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

Djikstra’s Shortest Path Example Find node K with smallest C[K] and add to { S } { S } = 1, 2, 3 C[1] = 0 C[2] = 5 C[3] = 6 C[4] = 15 C[5] = 

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

Djikstra’s Shortest Path Example Find node K with smallest C[K] and add to { S } { S } = 1, 2, 3, 4 C[1] = 0 C[2] = 5 C[3] = 6 C[4] = 9 C[5] = 

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

Djikstra’s Shortest Path Example Find node K with smallest C[K] and add to { S } { S } = 1, 2, 3, 4, 5 C[1] = 0 C[2] = 5 C[3] = 6 C[4] = 9 C[5] = 18

Djikstra’s Shortest Path Example No more nodes in { S } { S } = 1, 2, 3, 4, 5 C[1] = 0 C[2] = 5 C[3] = 6 C[4] = 9 C[5] = 18

Graph Implementation Representations Important for very large graphs 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 nj, nk 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 nj, nk implies nk, nj

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 ½ N2 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 Each edge stores reference to 2 nodes

Graph Time Requirements Complexity of operations For graph with N nodes, E edges Operation Adj Matrix Adj List Find edge O(1) O(E/N) Insert node Insert edge Delete node O(N) O(E) Delete edge