Graphs & Graph Algorithms 2

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

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.
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.
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
Chapter 3 The Greedy Method 3.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
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: 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.
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.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
Graphs & Graph Algorithms 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Graph Operations And Representation. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Graph. Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each decision is locally optimal. These.
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
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 5 Graph Algorithms.
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
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.
Runtime O(VE), for +/- edges, Detects existence of neg. loops
Union-Find  Application in Kruskal’s Algorithm  Optimizing Union and Find Methods.
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.
Graphs 황승원 Fall 2010 CSE, POSTECH. 2 2 Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
Graphs 2015, Fall Pusan National University Ki-Joune Li.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
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.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Chapter 5 : Trees.
Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008
CS120 Graphs.
Graph Operations And Representation
Data Structures & Algorithms Graphs
Minimum Spanning Tree.
Graphs & Graph Algorithms 2
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
CSE 373 Data Structures and Algorithms
Graph Operations And Representation
Chapter 11 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 .
Presentation transcript:

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

Overview Graph implementation Spanning trees Minimum spanning tree Adjacency list / matrix / set Spanning trees Minimum spanning tree Prim’s algorithm Kruskal’s algorithm

Graph Implementation How do we represent edges? Adjacency matrix 2D array of neighbors Adjacency list list of neighbors Adjacency set/map 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 or array list for each node of neighbors/successors for directed graph, may need predecessors as well Unweighted graph store neighbor Weighted graph store neighbor, weight

Adjacency List Example Unweighted graph Weighted graph

Adjacency Set/Map For each edge, store a Set or Map of neighbors/successors for directed graphs, may need separate Map for predecessors For unweighted graphs, use a Set For weighted graphs, use a Map from nodes to weights

Graph Space Requirements Adjacency matrix ½ N2 entries (for graph with N nodes, E edges) Many empty entries for large graphs Adjacency list E entries Adjacency Set/Map Space overhead per entry higher than for adjacency list

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

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

Recursive Spanning Tree Construction Known = { start } explore ( start ); void explore (Node X) { for each successor Y of X if (Y is not in Known) Parent[Y] = X Add Y to Known explore(Y)

Spanning Tree Construction Known = { start } Discovered = { start } while ( Discovered   ) take node X out of Discovered for each successor Y of X if (Y is not in Known) Parent[Y] = X Add Y to Discovered Add Y to Known

Breadth & Depth First Spanning Trees Breadth-first Depth-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)

Algorithms for MST Two well known algorithms for minimum spanning tree developed independently Prim’s algorithm described in book Kruskal’s algorithm Not Clyde Kruskal (prof in our department, but his uncle)

Shortest Path – Djikstra’s Algorithm S = {}, 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

MST – Prim’s Algorithm Optimal solution computed with greedy algorithm S = {}, 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

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

MST – Connected Subgraph Example

Union find algorithm/data structure Algorithm and data structure that allows you to ask this question. Start with n nodes, each in different subgraphs Two operations: Are nodes x and y in the same subgraph? Merge the subgraphs containing x and y

How fast is it? Ackermann’s function int A(x,y) { if (x == 0) return y+1; if (y == 0) return A(x-1, 1); return A(x-1, A(x, y-1)); A(2,2) = 7 A(3,3) = 61 A(4,2) = 265536 - 3 A(4,3) = 2265536 - 3 A(4,4) = 22265536 - 3

Inverse Ackermann’s function (n) is the inverse Ackermann’s function (n) = the smallest k s.t. A(k,k) >= n (number of atoms in universe) = 4 A sequence of n operations on a union find data structure requires O(n (n) ) time