10/13/2015IT 328, review graph algorithms1 Topological Sort ( topological order ) Let G = (V, E) be a directed graph with |V| = n. 1.{ v 1, v 2,.... v.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

CHAPTER 7 Greedy Algorithms.
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.
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.
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.
Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E.
Data Structures Using C++
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.
Chapter 23 Minimum Spanning Trees
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
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.
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.
CSE 421 Algorithms Richard Anderson Dijkstra’s algorithm.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CSE 326: Data Structures Spanning Trees Ben Lerner Summer 2007.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
CS 146: Data Structures and Algorithms July 21 Class Meeting
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
SPANNING TREES Lecture 21 CS2110 – Spring
Chapter 2 Graph Algorithms.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Module 5 – Networks and Decision Mathematics Chapter 23 – Undirected Graphs.
알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap4. Greedy Approach Grabs data items in sequence, each time with “best” choice, without.
Kruskal’s and Dijkstra’s Algorithm.  Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connected weighted.
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
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.
CSC2100B Tutorial 10 Graph Jianye Hao.
CSCI2100 Data Structures Tutorial 12
Graphs Upon completion you will be able to:
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:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
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.
Minimum Spanning Tree Chapter 13.6.
CISC 235: Topic 10 Graph Algorithms.
Graphs: As a mathematics branch
Topological Sort (topological order)
CS120 Graphs.
Topological Sort (topological order)
Short paths and spanning trees
Graph Algorithm.
Minimum-Cost Spanning Tree
Connected Components Minimum Spanning Tree
Graph Algorithm.
Graphs Chapter 13.
Minimum Spanning Trees
CSE 373 Data Structures and Algorithms
Graphs: As a mathematics branch
Topological Sort CSE 373 Data Structures Lecture 19.
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
CSCI2100 Data Structures Tutorial
Autumn 2016 Lecture 10 Minimum Spanning Trees
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.
Winter 2019 Lecture 10 Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
Minimum-Cost Spanning Tree
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:

10/13/2015IT 328, review graph algorithms1 Topological Sort ( topological order ) Let G = (V, E) be a directed graph with |V| = n. 1.{ v 1, v 2,.... v n } = V, and 2.for every i and j with 1  i < j  n, (v j, v i )  E A Topological Sort is a sequence v 1, v 2,.... v n, such that There are more than one such order in this graph!!

10/13/2015IT 328, review graph algorithms2 If the graph is not acyclic, then there is no topological sort for the graph Not an acyclic Graph No way to arrange vertices in this cycle without pointing backwards

10/13/2015IT 328, review graph algorithms3 Algorithm for finding Topological Sort Principle: vertex with in-degree 0 should be listed first. Algorithm: Repeat the following steps until no more vertex left: 1.Find a vertex v with in-degree 0; Input G=(V,E) 2. Remove v from V and update E 3. Add v to the end of the sort If can’t find such v and V is not empty then stop the algorithm; this graph is not acyclic

10/13/2015IT 328, review graph algorithms4 Topological Sort ( topological order ) of an acyclic graph

10/13/2015IT 328, review graph algorithms the graph is not acyclic Fail to find a topological sort ( topological order ) 028

10/13/2015IT 328, review graph algorithms This graph is acyclic Topological Sort ( topological order ) of an acyclic graph

10/13/2015IT 328, review graph algorithms7 Topological Sort in C++ // return an array where the first entry is the size of g // or 0 if g is not acyclic; int * graph::topsort(const Graph & g) { int *topo; Graph tempG = g; topo = new int[g.size()+1]; topo[0] = g.size(); for (int i=1; i<=topo[0]; i++) { int v = findVertexwithZeroIn(tempG); if (v == -1) { // g is not acyclic delete [] topo; topo = new int[1]; topo[0] = 0; return topo; } topo[i] = v; remove_v(tempG, v); } return topo; }

10/13/2015IT 328, review graph algorithms8 Edsger Wybe Dijkstra Dijkstra Algorithm: To find the shortest path between two vertices in a weighted graph. Algorithm design Programming languages Operating systems Distributed processing Formal specification and verification 1972 Turing Award

10/13/2015IT 328, review graph algorithms9 1.Back Tracking Greedy Algorithms: Algorithms that make decisions based on the current information; and once a decision is made, the decision will not be revised 3.Divide and Conquer Dynamic Programming..... Common Algorithm Techniques Problem: Minimized the number of coins for change. In real life, we can use a greedy algorithm to obtain the minimum number of coins. But in general, we need dynamic programming to do the job

10/13/2015IT 328, review graph algorithms10 Greedy Algorithms  Dynamic Programming Problem: Minimized the number of coins for 66 ¢ { 25¢, 10¢, 5¢, 1¢ } { 25¢, 12¢, 5¢, 1¢ } 25¢250¢ 10¢1 5¢1 1¢1 566¢ 25¢250¢ 12¢1 5¢00¢ 1¢44¢ 766¢ 25¢250¢ 12¢00¢ 5¢315¢ 1¢1 666¢ Greedy method Dynamic method 16¢ 6¢ 1¢ 0¢ 16¢ 4¢ 0¢

10/13/2015IT 328, review graph algorithms11 Unweighted Graphs  Weighted Graphs Starting Vertex w Final Vertex Starting Vertex w Final Vertex Both are using the greedy algorithm Finding the shortest path between two vertices

10/13/2015IT 328, review graph algorithms12 Shortest paths of unweighted graphs PathMap graph::UnweightedShortestPath(Graph & g, int s) { PathMap SPMap; // create a bookkeeper; for (Graph::iterator itr = g.begin(); itr != g.end(); itr++) { SPMap[itr->first] = make_pair(inft,-1); // -1 mean no previous } SPMap[s] = make_pair(0,-1); // s is not done yet. deque candidates; candidates.push_back(s); while (!candidates.empty()) { int v = candidates[0]; candidates.pop_front(); AdjacencyList ADJ = g[v]; for (AdjacencyList::iterator w=ADJ.begin(); w != ADJ.end(); w++) { if (SPMap[w->first].first == inft) { SPMap[w->first].first = SPMap[v].first+1; SPMap[w->first].second=v; candidates.push_back(w->first); } // end enqueue next v } // end ADJ interation; all nextvs's next into the queue. } return SPMap; } // > typedef map > PathMap;

10/13/2015IT 328, review graph algorithms13 Construct a shortest path Map for weighted graph Starting Vertex 5 n Final Vertex Starting Vertex Final Vertex 0,-1 5,0 7,13,1 10,2 2,0 w-1,y w,x  : decided    3,1    8,4 

10/13/2015IT 328, review graph algorithms14 Shortest paths of weighted graphs (I) // A Dijkstra's algorithm PathMap graph::ShortestPath(Graph & g, int s) { PathMap SPMap; map > dist_map; // A distance map // for book keeping; for (Graph::iterator itr = g.begin(); itr != g.end(); itr++) { dist_map[itr->first] = make_pair(inft,false); SPMap[itr->first] = make_pair(inft,-1); } dist_map[s] = make_pair(0,false); // s is not done yet. multimap candidates ; // next possible vertices //sorted by their distance. candidates.insert(make_pair(0,s)); // start from s;..... }

10/13/2015IT 328, review graph algorithms15 Shortest paths (II) // A Dijkstra's algorithm PathMap graph::ShortestPath(Graph & g, int s) { while (! candidates.empty()) { int v = candidates.begin()->second; double costs2v = candidates.begin()->first; candidates.erase(candidates.begin()); if (dist_map[v].second) continue; // v is done after pair // (weight v) is inserted; dist_map[v] = make_pair(costs2v,true); AdjacencyList ADJ = g[v]; // all not done adjacent vertices // should be candidates for (AdjacencyList::iterator itr=ADJ.begin(); itr != ADJ.end(); itr++) { int w = itr->first; if (dist_map[w].second) continue; // this w is done; double cost_via_v = costs2v + itr->second; if (cost_via_v < dist_map[w].first) { dist_map[w] = make_pair(cost_via_v,false); SPMap[w] = make_pair(cost_via_v,v); } candidates.insert(make_pair(cost_via_v,w)); } // end for ADJ iteration; } // end while !candidates.empty() return SPMap; }

10/13/2015IT 328, review graph algorithms16 Minimum Spanning Tree of an undirected graph: The lowest cost to connect all vertices Since a cycle is not necessary, such a connection must be a tree. A spanning is a walk such that every vertex is included

10/13/2015IT 328, review graph algorithms17 Prim’s algorithm:grow the minimum spanning tree from a root

10/13/2015IT 328, review graph algorithms18 Prim’s algorithm:grow the minimum spanning tree from a different root

10/13/2015IT 328, review graph algorithms19 Kruskal’s Algorithm: construct the minimum spanning from edges

10/13/2015IT 328, review graph algorithms20 A Hamiltonian cycle is a cycle that contains every vertex; A graph that contains a Hamiltonian cycle is called Hamiltonian

10/13/2015IT 328, review graph algorithms21 A non-Hamiltonian graph