Graphs & Graph Algorithms 2 Fawzi Emad Chau-Wen Tseng 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.
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.
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.
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.
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 & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
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.
Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Edges are.
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.
Graph Operations And Representation. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
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.
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
Representing and Using Graphs
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.
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.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
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 A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
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.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
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.
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.
Graph Operations And Representation
Minimum Spanning Tree.
Graphs & Graph Algorithms 2
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
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.
Graphs.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
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 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-firstDepth-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? 1. Traversal approach 1. Traverse tree starting at X 2. Cycle if reach Y 2. Connected subgraph approach 1. Maintain set of nodes for each connected subgraph 2. Initialize one connected subgraph for each node 3. When edge (X,Y) is added to tree Merge sets containing X, Y 4. Cycle if X,Y in same set

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 { S } =  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 ( , ) = 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 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)