UNCA CSCI 363 14 November, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
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.
1 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Fundamentals of Python: From First Programs Through Data Structures
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.
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.
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.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
IS 2610: Data Structures Graph April 5, 2004.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
CS112A1 Spring 2008 Practice Final. ASYMPTOTIC NOTATION: a)Show that log(n) and ln(n) are the same in terms of Big-Theta notation b)Show that log(n+1)
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Course notes CS2606: Data Structures and Object-Oriented Development Graphs Department of Computer Science Virginia Tech Spring 2008 (The following notes.
Minimum Spanning Trees
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Computer Science 112 Fundamentals of Programming II Graph Algorithms.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
SPANNING TREES Lecture 21 CS2110 – Spring
E.G.M. PetrakisGraphs1  Graph G(V,E): finite set V of nodes (vertices) plus a finite set E of arcs (or edges) between nodes  V = {A, B, C, D, E, F, G}
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,
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
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.
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.
Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an.
Runtime O(VE), for +/- edges, Detects existence of neg. loops
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
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++
Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
E.G.M. PetrakisGraphs1 Graph G(V,E): finite set V of nodes (vertices) plus a finite set E of arcs (or edges) between nodes –V = {A, B, C, D, E, F, G} –E.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
CSCI2100 Data Structures Tutorial 12
Minimum- Spanning Trees
Graphs 2015, Fall Pusan National University Ki-Joune Li.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington These slides are.
Graph Search Applications, Minimum Spanning Tree
Graphs Representation, BFS, DFS
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Short paths and spanning trees
Graphs Representation, BFS, DFS
Minimum Spanning Tree.
CSCI 333 Graphs Chapter 11 20, 22, and 25 November 2002.
Chapter 11 Graphs.
Autumn 2015 Lecture 10 Minimum Spanning Trees
Minimum Spanning Tree Algorithms
CSCI2100 Data Structures Tutorial
Algorithms Searching in a Graph.
Weighted Graphs & Shortest Paths
CSE 417: Algorithms and Computational Complexity
CSE 373: Data Structures and Algorithms
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
GRAPH – Definitions A graph G = (V, E) consists of
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:

UNCA CSCI November, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright © 2000, 2001

Graph Traversals (2) To insure visiting all vertices: void graphTraverse(const Graph* G) { for (v=0; v n(); v++) G->setMark(v, UNVISITED); // Initialize for (v=0; v n(); v++) if (G->getMark(v) == UNVISITED) doTraverse(G, v); }

Depth First Search (1) // Depth first search void DFS(Graph* G, int v) { PreVisit(G, v); // Take action G->setMark(v, VISITED); for (int w=G->first(v); w n(); w = G->next(v,w)) if (G->getMark(w) == UNVISITED) DFS(G, w); PostVisit(G, v); // Take action }

Depth First Search (2) Cost:  (|V| + |E|).

Breadth First Search (1) Like DFS, but replace stack with a queue. –Visit vertex’s neighbors before continuing deeper in the tree.

Breadth First Search (2) void BFS(Graph* G, int start,Queue *Q) { int v, w; Q->enqueue(start); // Initialize Q G->setMark(start, VISITED); while (Q->length() != 0) { // Process Q Q->dequeue(v); PreVisit(G, v); // Take action for(w=G->first(v);w n();w=G->next(v,w)) if (G->getMark(w) == UNVISITED) { G->setMark(w, VISITED); Q->enqueue(w); } PostVisit(G, v); // Take action }

Breadth First Search (3)

Topological Sort (1) Problem: Given a set of jobs, courses, etc., with prerequisite constraints, output the jobs in an order that does not violate any of the prerequisites.

Topological Sort (2) void topsort(Graph* G) { // Topological sort int i; for (i=0; i n(); i++) // Initialize G->setMark(i, UNVISITED); for (i=0; i n(); i++) // Do vertices if (G->getMark(i) == UNVISITED) tophelp(G, i); // Call helper } void tophelp(Graph* G, int v) { // Process v G->setMark(v, VISITED); for (int w=G->first(v); w n(); w = G->next(v,w)) if (G->getMark(w) == UNVISITED) tophelp(G, w); printout(v); // PostVisit for Vertex v }

Topological Sort (3)

Queue-Based Topsort void topsort(Graph* G, Queue * Q) { int Count[G->n()]; int v, w; for (v=0; v n(); v++) Count[v] = 0; for (v=0; v n(); v++) // Process edges for (w=G->first(v); w n(); w = G->next(v,w)) Count[w]++; // Add to v2's count for (v=0; v n(); v++) // Initialize Q if (Count[v] == 0) // No prereqs Q->enqueue(v); while (Q->length() != 0) { Q->dequeue(v); printout(v); // PreVisit for V for (w=G->first(v); w n(); w = G->next(v,w)) { Count[w]--; // One less prereq if (Count[w] == 0) // Now free Q->enqueue(w); }}}

Shortest Paths Problems Input: A graph with weights or costs associated with each edge. Output: The list of edges forming the shortest path. Sample problems: –Find shortest path between two named vertices –Find shortest path from S to all other vertices –Find shortest path between all pairs of vertices Will actually calculate only distances.

Shortest Paths Definitions d(A, B) is the shortest distance from vertex A to B. w(A, B) is the weight of the edge connecting A to B. –If there is no such edge, then w(A, B) = .

Single-Source Shortest Paths Given start vertex s, find the shortest path from s to all other vertices. Try 1: Visit vertices in some order, compute shortest paths for all vertices seen so far, then add shortest path to next vertex x. Problem: Shortest path to a vertex already processed might go through x. Solution: Process vertices in order of distance from s.

Dijkstra’s Algorithm Example ABCDE Initial0  Process A  Process C Process B Process D Process E

Dijkstra’s Implementation // Compute shortest path distances from s, // return them in D void Dijkstra(Graph* G, int* D, int s) { int i, v, w; for (i=0; i n(); i++) { // Do vertices v = minVertex(G, D); if (D[v] == INFINITY) return; G->setMark(v, VISITED); for (w=G->first(v); w n(); w = G->next(v,w)) if (D[w] > (D[v] + G->weight(v, w))) D[w] = D[v] + G->weight(v, w); }

Implementing minVertex Issue: How to determine the next-closest vertex? (I.e., implement minVertex ) Approach 1: Scan through the table of current distances. –Cost:  (|V| 2 + |E|) =  (|V| 2 ). Approach 2: Store unprocessed vertices using a min-heap to implement a priority queue ordered by D value. Must update priority queue for each edge. –Cost:  ((|V| + |E|)log|V|)

Approach 1 // Find min cost vertex int minVertex(Graph* G, int* D) { int i, v; // Set v to an unvisited vertex for (i=0; i n(); i++) if (G->getMark(i) == UNVISITED) { v = i; break; } // Now find smallest D value for (i++; i n(); i++) if ((G->getMark(i) == UNVISITED) && (D[i] < D[v])) v = i; return v; }

Approach 2 void Dijkstra(Graph* G, int* D, int s) { int i, v, w; // v is current vertex DijkElem temp; DijkElem E[G->e()]; // Heap array temp.distance = 0; temp.vertex = s; E[0] = temp; // Initialize heap array minheap H(E, 1, G->e()); for (i=0; i n(); i++) {// Get distances do { if(!H.removemin(temp)) return; v = temp.vertex; } while (G->getMark(v) == VISITED); G->setMark(v, VISITED); if (D[v] == INFINITY) return; for(w=G->first(v); w n(); w=G->next(v,w)) if (D[w] > (D[v] + G->weight(v, w))) { D[w] = D[v] + G->weight(v, w); temp.distance = D[w]; temp.vertex = w; H.insert(temp); // Insert in heap }}}

All-Pairs Shortest Paths For every vertex u, v  V, calculate d(u, v). Could run Dijkstra’s Algorithm |V| times. Better is Floyd’s Algorithm. Define a k-path from u to v to be any path whose intermediate vertices all have indices less than k.

Floyd’s Algorithm //Floyd's all-pairs shortest paths algorithm void Floyd(Graph* G) { int D[G->n()][G->n()]; // Store distances for (int i=0; i n(); i++) // Initialize for (int j=0; j n(); j++) D[i][j] = G->weight(i, j); // Compute all k paths for (int k=0; k n(); k++) for (int i=0; i n(); i++) for (int j=0; j n(); j++) if (D[i][j] > (D[i][k] + D[k][j])) D[i][j] = D[i][k] + D[k][j]; }

Minimal Cost Spanning Trees Minimal Cost Spanning Tree (MST) Problem: Input: An undirected, connected graph G. Output: The subgraph of G that 1) has minimum total cost as measured by summing the values of all the edges in the subset, and 2) keeps the vertices connected.

MST Example

Prim’s MST Algorithm void Prim(Graph* G, int* D, int s) { int V[G->n()]; // Who's closest int i, w; for (i=0; i n(); i++) {// Do vertices int v = minVertex(G, D); G->setMark(v, VISITED); if (v != s) AddEdgetoMST(V[v], v); if (D[v] == INFINITY) return; for (w=G->first(v); w n(); w = G->next(v,w)) if (D[w] > G->weight(v,w)) { D[w] = G->weight(v,w);// Update dist V[w] = v; // Update who it came from }

Alternate Implementation As with Dijkstra’s algorithm, the key issue is determining which vertex is next closest. As with Dijkstra’s algorithm, the alternative is to use a priority queue. Running times for the two implementations are identical to the corresponding Dijkstra’s algorithm implementations.

Kruskal’s MST Algorithm (1) Initially, each vertex is in its own MST. Merge two MST’s that have the shortest edge between them. –Use a priority queue to order the unprocessed edges. Grab next one at each step. How to tell if an edge connects two vertices already in the same MST? –Use the UNION/FIND algorithm with parent- pointer representation.

Kruskal’s MST Algorithm (2)

Kruskal’s MST Algorithm (3) Cost is dominated by the time to remove edges from the heap. –Can stop processing edges once all vertices are in the same MST Total cost:  (|V| + |E| log |E|).