More Graph Algorithms Weiss ch. 14. 2 Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.

Slides:



Advertisements
Similar presentations
Chapter 5: Tree Constructions
Advertisements

Lecture 15. Graph Algorithms
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.
1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE Lectures 20 – Intro to Graphs.
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.
Chapter 3 The Greedy Method 3.
Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints.
CPSC 411, Fall 2008: Set 9 1 CPSC 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Fall 2008.
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.
Minimum Spanning Trees Definition Algorithms –Prim –Kruskal Proofs of correctness.
CSE 421 Algorithms Richard Anderson Dijkstra’s algorithm.
Chapter 9: Graphs Summary Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
Shortest Path Algorithms
CSE 421 Algorithms Richard Anderson Lecture 10 Minimum Spanning Trees.
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.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Minimum Spanning Trees
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
Week -7-8 Topic - Graph Algorithms CSE – 5311 Prepared by:- Sushruth Puttaswamy Lekhendro Lisham.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Graph (II) Shortest path, Minimum spanning tree GGuy
Charles Lin. Graph Representation Graph Representation DFS DFS BFS BFS Dijkstra Dijkstra A* Search A* Search Bellman-Ford Bellman-Ford Floyd-Warshall.
Chapter 2 Graph Algorithms.
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
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.
Graphs David Kauchak cs302 Spring DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch.
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.
Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Runtime O(VE), for +/- edges, Detects existence of neg. loops
1 Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible b locally optimal.
Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
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.
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Minimum Spanning Trees and Shortest Paths
Short paths and spanning trees
More Graph Algorithms.
Graph Algorithm.
Minimum-Cost Spanning Tree
Minimum Spanning Tree.
Graphs & Graph Algorithms 2
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
CSE 373 Data Structures and Algorithms
Minimum-Cost Spanning Tree
Minimum-Cost Spanning Tree
Chapter 11 Graphs.
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
CSCI2100 Data Structures Tutorial
Minimum Spanning Tree.
Autumn 2016 Lecture 10 Minimum Spanning Trees
CSE 417: Algorithms and Computational Complexity
CSE 373: Data Structures and Algorithms
Lecture 14 Minimum Spanning Tree (cont’d)
Topological Sorting Minimum Spanning Trees Shortest Path
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 .
Presentation transcript:

More Graph Algorithms Weiss ch. 14

2 Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not yet examined. Select it so long as it does not create a cycle with edges selected so far. toDo  all edges in graph while (num selected < |V|-1 && toDo not empty) { c  find and remove smallest edge from toDo if (selected plus c has no cycles) selected.insert(c) What data structures are needed? What is running time?

3 Kruskal’s Algorithm Can sort edges in toDo set first –O(|E| log |E|) for sort –O(1) for finding and removing minimum (at most |E| times) –O(|E| log |E|) overall contribution to running time Alternatively, put them in a heap –O(|E|) for building the heap –O(log |E|) for finding and removing minimum (at most |E| times) –O(|E| log |E|) overall contribution to running time In practice? –How many edges are examined?  influences choice of heap vs. sorting

4 Kruskal’s Algorithm: Checking for Cycles At each step, we have a set of selected edges, and one new edge. (Say there are n edges together.) Need to check if together they form any cycles. How?

5 Checking for Cycles Adding edge (u, v) Do a graph search using edges selected already –Look for a path from u to v –Slow – O(|E|) worst case at every step  O(|E| * |E|) Better: keep track of which “component” each node is in – can we do it in O(log |E|) ?  O(|E| log |E|) –Iff u and v in same component, then cycle would be formed –Update “component” labels whenever new edge added component of u and component of v merged into a single component –There is a data structure for doing this union/find details beyond cs211

6 Graph Sort Orders A sort order is just some ordering of the nodes in a graph. We have seen: –BFS ordering – in order of (some) BFS traversal from A –DFS ordering – in order of (some) DFS traversal from A –Minimum path ordering – in order of min distance to A A move flexible ordering is possible for directed graphs…

7 Topological Sort Directed graph G. Rule: if there is an edge u  v, then u must come before v. Ex: AB C DE F G HI AB C DE F G HI

8 Intuition Cycles make topological sort impossible. Select any node with no in-edges –print it –delete it –and delete all the edges leaving it Repeat What if there are some nodes left over? Implementation? Efficiency?

9 Implementation Start with a list of nodes with in-degree = 0 Select any edge from list –mark as deleted –mark all outgoing edges as deleted –update in-degree of the destinations of those edges If any drops below zero, add to the list Running time?

10 Implementation Start with a list of nodes with in-degree = 0 Select any edge from list –mark as deleted –mark all outgoing edges as deleted –update in-degree of the destinations of those edges If any drops below zero, add to the list Running time? In all, algorithm does work: –O(|V|) to construct initial list –Every edge marked “deleted” at most once: O(|E|) total –Every node marked “deleted” at most once: O(|V|) total –So linear time overall (in |E| and |V|)

11 Why should we care? Shortest path problem in directed, acyclic graph –Called a DAG for short General problem: –Given a DAG input G with weights on edges –Find shortest paths from source A to every other vertex

12 Naïve Solution Just do Dijkstra’s algorithm, with modification to support directed edges –O(|E| log |E|) We can do better. (much better).

13 Example AB C DE F G HI A B C DE F G HI

14 Algorithm Given a DAG G, and a source vertex A Do topological sort on G, and visit nodes in that order Nodes before A get distance negative infinity Node A gets distance 0 Each node u after A computes distance based on incoming edges (all of which have sources before u in the list, and so are already computed). Does it work for negative weights? A B C DE F G HI One sort order: A F I E H G D B C A = -inf F = -inf I = 0 E = 3 + F or 1 + I = 1 H = 2 + I = 2 G = 3 + A or 2 + I or 1 + H = 2 …

15 Algorithmic Complexity Our algorithm on a directed acyclic graph (possibly with negative weights) –Clearly linear time in |V| + |E| –Topological sort is linear time –Then examine each node and edge once more Bellman-Ford on possibly cyclic directed graph with possibly negative weights –Similar to our algorithm, but no topological sort –Needs to examine every node & edge on every pass When examinning node v, update estimate by looking at every edge u  v –O(|V| |E|) –But: can detect and handle cycles Each node v examined at most |V| times… why?