Graphs 2 Kevin Kauffman CS 309s. Problem We have sprinklers in our yard which need to be connected with pipe. Assuming pipes may only intersect at sprinkler.

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
1 Discrete Structures & Algorithms Graphs and Trees: III EECE 320.
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
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.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
DAST 2005 Tirgul 14 (and more) sample questions. DAST 2005 (reminder?) Kruskal’s MST Algorithm.
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.
Graphs & Graph Algorithms 2
CSE 421 Algorithms Richard Anderson Lecture 10 Minimum Spanning Trees.
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
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.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Graphs & Graph Algorithms 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
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.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
CS 146: Data Structures and Algorithms July 21 Class Meeting
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
Charles Lin. Graph Representation Graph Representation DFS DFS BFS BFS Dijkstra Dijkstra A* Search A* Search Bellman-Ford Bellman-Ford Floyd-Warshall.
COSC 2007 Data Structures II Chapter 14 Graphs III.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
All Pair Shortest Path IOI/ACM ICPC Training June 2004.
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.
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
Runtime O(VE), for +/- edges, Detects existence of neg. loops
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 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.
Minimum- Spanning Trees
Graphs Upon completion you will be able to:
Contest Algorithms January 2016 Describe shortest path trees, SSSP, APSP, and three algorithms: Dijkstra, Bellman-Ford, Floyd-Warshall 9. Shortest Paths.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
Kruskal’s Algorithm for Computing MSTs Section 9.2.
Graph Search Applications, Minimum Spanning Tree
Graphs Representation, BFS, DFS
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Shortest Paths and Minimum Spanning Trees
Shortest Path from G to C Using Dijkstra’s Algorithm
Minimum Spanning Trees and Shortest Paths
Graphs Representation, BFS, DFS
Graph Algorithm.
Graphs & Graph Algorithms 2
CSE 373 Data Structures and Algorithms
Minimum Spanning Tree Algorithms
Shortest Paths and Minimum Spanning Trees
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Algorithms: Design and Analysis
Weighted Graphs & Shortest Paths
Autumn 2016 Lecture 10 Minimum Spanning Trees
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
CSE 373: Data Structures and Algorithms
Graphs: Shortest path and mst
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Graph Algorithm.
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 2 Kevin Kauffman CS 309s

Problem We have sprinklers in our yard which need to be connected with pipe. Assuming pipes may only intersect at sprinkler heads, what is the minimum amount of pipe we need to connect the sprinklers?

Minimum Spanning Tree Set of edges which – Connect all the nodes – Has the minimum total weight of all edges Contains no cycles If there are multiple MSTs for a given graph, they all contain the same individual edge weights If on an xy plane, it will not self intersect

Calculating the MST Create a set of connected nodes Add a random node to “seed” the set While the set doesn’t contain all nodes – Find the shortest edge which goes from a node in the set to a node out of the set – Add the edge to the MST and the other node to the set

Implementation Naïve: iterate through all edges in each cycle, and take the minimum which satisfies the in/out requirement – O(V*E) Better: maintain sorted list of edges which touch the nodes in the set, add to it as we go – O(E*log(E))

Maintaining Sorted List Use int[2] or java.awt.point to represent edges Place them on a TreeSet when we see them, which will sort them in the order specified by our: Custom Comparator

Takes input A and B; returns an int – Returns positive if A comes before B – Returns 0 if A is the same as B – Returns negative if A comes after B Rules: – Commutative: if comp(A,B) 0 – MUST break ties (or comparator will think A and B are the same and delete one of them) This must still adhere to rule 1

Shortest Distance BFS allows us to calculate distance in unweighted graphs, how do we do it in weighted graphs? For an edge of length n, insert n-1 “fake” nodes, so all edges are length 1 Graph is now unweighted Run BFS!

A Better Way If we add nodes proportional to edge length, runtime now depends on edge length (bad for long edges) Instead of iterating over all the fake nodes can we calculate the “real” node which we are going to reach next?

Dijkstra’s Algorithm Lemma: if we are at a node, we can calculate the next node BFS would visit by comparing edge lengths Start at start node Sort edges from start node to find next node visited (second node) – How do we find the the third node, since it could be connected to the second node, but not the start node?

Dijkstra’s Algorithm Because of Lemma, we can calculate the distance from the second node to all the nodes its connected to Total distance to those nodes is distance(2 nd node) + weight(edge) – EXCEPT if we had a shorter distance directly from the start node Sort all those distances and find the actual third node visited REPEAT

Dijkstra’s TL-DR Tree set of nodes by distance Pop shortest remaining distance (u), make it permanent for all edges from u to an unvisited node (v), if dist(u)+dist(uv)<dist(v), update dist(v) O((E+V)log(E)), doesn’t depend on edge length

Implementation Array storing distances (must be final since we reference it in the compartor) Treeset sorting unvisited nodes by distance Every time we pop a node and check all its edges, if we find that ends up being less than current distance: – Remove that node from the treeset – Update its value – Add it to the treeset TREESET DOESN’T LIKE VALUES CHANGED UNDER IT