Topological Sort Algorithm

Slides:



Advertisements
Similar presentations
Bellman-Ford algorithm
Advertisements

0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Single Source Shortest Paths
Chapter 9: Graphs Shortest Paths
Graph Theory Arnold Mesa. Basic Concepts n A graph G = (V,E) is defined by a set of vertices and edges v3 v1 v2Vertex (v1) Edge (e1) A Graph with 3 vertices.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Graph Algorithms. Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 2 Outline Graph Representation Shortest path Minimum spanning trees.
CMSC 341 Graphs 3. Adjacency Table Implementation Uses table of size |V|  |V| where each entry (i,j) is boolean –TRUE if there is an edge from vertex.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
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 Structures Using C++
1 9.3 Shortest Path Algorithms Improvement –Use a queue to keep all vertices whose shortest distance to s is known. –Initialize d v to  for all vertices,
1 Graph Theory © Dave Bockus. 2 Adjacency Matrix
CMSC 341 Graphs 2. 2 Weighted Shortest Path Problem Single-source shortest-path problem: Given as input a weighted graph, G = (V,E), and a distinguished.
 Graph Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
1 Shortest Path Algorithms Given a graph G = (V, E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. weighted.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
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.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Shortest Path Algorithm By Weston Vu CS 146. What is Shortest Paths? Shortest Paths is a part of the graph algorithm. It is used to calculate the shortest.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.
Using Dijkstra’s Algorithm to Find a Shortest Path from a to z 1.
CISC 235: Topic 11 Shortest Paths Algorithms. CISC 235 Topic 112 Outline Single-Source Shortest Paths Algorithm for Unweighted Graphs Algorithm for Weighted,
Dijkstra’s Algorithm. 2 Shortest-path Suppose we want to find the shortest path from node X to node Y It turns out that, in order to do this, we need.
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Graph Algorithms Terminology Topological sort Shortest-path algorithms
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,
CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun.
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.
CMSC 341 Graphs. 8/3/2007 UMBC CMSC 341 Graphs 2 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
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 – Adjacency Matrix
CMSC 341 Graphs. 5/5/20062 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of edges, E. Each edge.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
Graphs Chapter 20.
A vertex u is reachable from vertex v iff there is a path from v to u.
C.Eng 213 Data Structures Graphs Fall Section 3.
Graphs: As a mathematics branch
Graphs.
CS120 Graphs.
Topological Sort (topological order)
More Graph Algorithms.
CSC 172 DATA STRUCTURES.
Graph Algorithm.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
MST - Prim’s Algorithm Greedy algorithm that “grows” an MST by repeatedly finding the lowest cost edge that will connect a new vertex to MST For every.
CMSC 341 Graphs 3.
CMSC 341 Graphs 3.
CMSC 341 Lecture 20.
Graphs – Adjacency Matrix
Shortest Path Algorithms
A path that uses every vertex of the graph exactly once.
CSCI2100 Data Structures Tutorial
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Fundamental Structures of Computer Science II
Graphs G = (V, E) V are the vertices; E are the edges.
CMSC 341 Graphs 2.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 9: Graphs Shortest Paths
GRAPH – Definitions A graph G = (V, E) consists of
Presentation transcript:

Topological Sort Algorithm Given a graph G with n vertices, generate a list L of vertices arranged in topological order. Suppose that at the start, L is empty, and wasVisited is false for all vertices. // set indegree for each vertex (adj. matrix representation) for(i = 0; i < n; i++) for(j = 0; j < n; j++) v[j].indegree += A[i][j]; // apply topological sort { find an unvisited vertex v with no predecessors; if(v != null) // there is one with no predecessors insert v at back of L; v.wasVisited = true; decrement indegree of each vertex adjacent to v; } Algorithm to find an unvisited vertex with no predecessors: if((v[i].indegree == 0) && (v[i].wasVisited == false)) return v[i]; return null; // every vertex has a predecessor COSC 2P03 Week 9

Bridges of Konigsberg Problem There were 7 bridges in Konigsberg, arranged as above (pictures from http://mathworld.wolfram.com) Is it possible to travel across every bridge exactly once, and return to the starting point? COSC 2P03 Week 9

Weighted Graphs Each edge has an associated weight Adjacency matrix representation: A[i][j] = w if there is an edge between vertex i and vertex j with weight w A[i][j] = ∞ otherwise A B C D E F - 4 3 5  9 10 8 6 4 9 A B E 5 10 3 6 C D F 4 8 COSC 2P03 Week 9

Unweighted Shortest Path Algorithm We will find the shortest path from s to all other vertices. Assume that for every vertex v, we have initialized v.known = false and v.dist = ∞. s.dist = 0; Q.enqueue(s); while Q is not empty { v = Q.dequeue(); v.known = true; for each vertex w adjacent to v if(w.dist > v.dist+1) w.dist = v.dist + 1; w.path = v; Q.enqueue(w); } COSC 2P03 Week 9

Weighted Shortest Path Algorithm (Dijkstra’s Algorithm) We will find the weighted shortest path from s to all other vertices. Assume for every vertex v, we have initialized v.known = false and v.dist = ∞ s.dist = 0; PQ.enqueue(s, 0); while PQ is not empty { v = PQ.dequeue(); if(!v.known) v.known = true; for (each vertex w adjacent to v) if(w.dist > v.dist + A[v][w]) w.dist = v.dist + A[v][w]; w.path = v; PQ.enqueue(w, w.dist); } COSC 2P03 Week 9