Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch.

Slides:



Advertisements
Similar presentations
CSE 2331/5331 CSE 780: Design and Analysis of Algorithms Lecture 14: Directed Graph BFS DFS Topological sort.
Advertisements

Single Source Shortest Paths
Graph Traversals and Minimum Spanning Trees. Graph Terminology.
Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.
1 Searching in a Graph Jeff Edmonds York University COSC 3101 Lecture 5 Generic Search Breadth First Search Dijkstra's Shortest Paths Algorithm Depth First.
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE Lectures 20 – Intro to Graphs.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
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.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all.
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
Lecture 10 Topics Application of DFS Topological Sort
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)
Shortest Paths Definitions Single Source Algorithms
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
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.
Data Structures and Algorithms
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
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.
CS 312: Algorithm Analysis Lecture #16: Strongly Connected Components This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported.
Topological Sorting and Least-cost Path Algorithms.
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.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
CS223 Advanced Data Structures and Algorithms 1 The Bellman-Ford Shortest Path Algorithm Neil Tang 03/11/2010.
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.
David Luebke 1 9/15/2015 CS 332: Algorithms Topological Sort Minimum Spanning Trees.
David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree.
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest)
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.
Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
ADA: 9. Graph Search1 Objective o describe and compare depth-first and breadth- first graph searching, and look at the creation of spanning trees.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
7. Graph Traversal Describe and compare depth-first and breadth-first graph searching, and look at the creation of spanning trees Contest Algorithms: 7.
1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
Graphs David Kauchak cs302 Spring Admin HW 12 and 13 (and likely 14) You can submit revised solutions to any problem you missed Also submit your.
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Chapter 22 Elementary Graph Algorithms
Shortest Paths and Minimum Spanning Trees
CSE 2331/5331 Topic 9: Basic Graph Alg.
Topological Sort Minimum Spanning Tree
Minimum Spanning Tree Shortest Paths
CSC 413/513: Intro to Algorithms
More Graph Algorithms.
Many slides here are based on E. Demaine , D. Luebke slides
CS 3343: Analysis of Algorithms
Many slides here are based on D. Luebke slides
Advanced Algorithms Analysis and Design
Shortest Paths and Minimum Spanning Trees
Algorithms CSCI 235, Spring 2019 Lecture 35 Graphs IV
Lecture 12 Shortest Path.
Chapter 22: Elementary Graph Algorithms III
Presentation transcript:

Graphs David Kauchak cs302 Spring 2012

DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Topological sort A linear ordering of all the vertices such that for all edges (u,v)  E, u appears before v in the ordering An ordering of the nodes that “obeys” the dependencies, i.e. an activity can’t happen until it’s dependent activities have happened underwear pants belt shirt tie jacket socks shoes watch underwear pants belt watch shirt tie socks shoes jacket

Topological sort

underwear pants belt shirt tie jacket socks shoes watch

Topological sort underwear pants belt shirt tie jacket socks shoes watch

Topological sort underwear pants belt shirt tie jacket socks shoes watch

Topological sort underwear pants belt shirt tie jacket socks shoes watch

Topological sort underwear pants belt shirt tie jacket socks shoes watch

Topological sort underwear pants belt shirt tie jacket socks shoes watch

Topological sort underwear pants belt shirt tie jacket socks shoes watch

Topological sort underwear pants belt shirt tie jacket socks shoes watch …

Running time?

O(|V|+|E|)

Running time? O(E) overall

Running time? How many calls? |V|

Running time? Overall running time? O(|V| 2 +|V| |E|)

Can we do better?

Topological sort 2

Running time? How many times do we process each node? How many times do we process each edge? O(|V| + |E|)

Connectedness Given an undirected graph, for every node u  V, can we reach all other nodes in the graph? Run BFS or DFS-Visit (one pass) and mark nodes as we visit them. If we visit all nodes, return true, otherwise false. Running time:O(|V| + |E|)

Strongly connected Given a directed graph, can we reach any node v from any other node u? Ideas?

Transpose of a graph Given a graph G, we can calculate the transpose of a graph G R by reversing the direction of all the edges A B C E D A B C E D G GRGR Running time to calculate G R ?O(|V| + |E|)

Strongly connected

Is it correct? What do we know after the first pass? Starting at u, we can reach every node What do we know after the second pass? All nodes can reach u. Why? We can get from u to every node in G R, therefore, if we reverse the edges (i.e. G), then we have a path from every node to u Which means that any node can reach any other node. Given any two nodes s and t we can create a path through u sut ……

Runtime? O(|V| + |E|) O(|V|)

Detecting cycles Undirected graph BFS or DFS. If we reach a node we’ve seen already, then we’ve found a cycle Directed graph A B D have to be careful

Detecting cycles Undirected graph BFS or DFS. If we reach a node we’ve seen already, then we’ve found a cycle Directed graph Call TopologicalSort If the length of the list returned ≠ |V| then a cycle exists

Shortest paths What is the shortest path from a to d? A B CE D

Shortest paths BFS A B CE D

Shortest paths What is the shortest path from a to d? A B CE D

Shortest paths We can still use BFS A B CE D

Shortest paths We can still use BFS A B CE D A B CE D

Shortest paths We can still use BFS A B CE D

Shortest paths What is the problem? A B CE D

Shortest paths Running time is dependent on the weights A B C A B C

Shortest paths A B C A B C

Shortest paths A B C

A B C

A B C Nothing will change as we expand the frontier until we’ve gone out 100 levels

Dijkstra’s algorithm

prev keeps track of the shortest path

Dijkstra’s algorithm

Single source shortest paths All of the shortest path algorithms we’ll look at today are call “single source shortest paths” algorithms Why?

A B CE D

A B CE D    

A B CE D    0 Heap A 0 B  C  D  E 

A B CE D    0 Heap B  C  D  E 

A B CE D    0 Heap B  C  D  E 

A B CE D   1 0 Heap C 1 B  D  E 

A B CE D   1 0 Heap C 1 B  D  E 

A B CE D   1 0 Heap C 1 B 3 D  E 

A B CE D   1 0 Heap C 1 B 3 D  E 

3 A B CE D   1 0 Heap B 3 D  E 

3 A B CE D   1 0 Heap B 3 D  E 

3 A B CE D   1 0 Heap B 3 D  E 

3 A B CE D   1 0 Heap B 2 D  E 

3 A B CE D   1 0 Heap B 2 D  E 

3 A B CE D  Heap B 2 E 5 D 

3 A B CE D  Heap B 2 E 5 D  Frontier?

3 A B CE D  Heap B 2 E 5 D  All nodes reachable from starting node within a given distance

3 A B CE D Heap E 3 D 5

3 A B CE D Heap D 5

3 A B CE D Heap

A B CE D

Is Dijkstra’s algorithm correct? Invariant:

Is Dijkstra’s algorithm correct? Invariant: For every vertex removed from the heap, dist[v] is the actual shortest distance from s to v proof?

Is Dijkstra’s algorithm correct? Invariant: For every vertex removed from the heap, dist[v] is the actual shortest distance from s to v The only time a vertex gets visited is when the distance from s to that vertex is smaller than the distance to any remaining vertex Therefore, there cannot be any other path that hasn’t been visited already that would result in a shorter path

Running time?

1 call to MakeHeap

Running time? |V| iterations

Running time? |V| calls

Running time? O(|E|) calls

Running time? Depends on the heap implementation 1 MakeHeap|V| ExtractMin|E| DecreaseKeyTotal ArrayO(|V|)O(|V| 2 ) O(|E|) O(|V| 2 ) Bin heapO(|V|)O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|) O(|E| log |V|)

Running time? Depends on the heap implementation 1 MakeHeap|V| ExtractMin|E| DecreaseKeyTotal ArrayO(|V|)O(|V| 2 ) O(|E|) O(|V| 2 ) Bin heapO(|V|)O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|) O(|E| log |V|) Is this an improvement?If |E| < |V| 2 / log |V|

Running time? Depends on the heap implementation 1 MakeHeap|V| ExtractMin|E| DecreaseKeyTotal ArrayO(|V|)O(|V| 2 ) O(|E|) O(|V| 2 ) Bin heapO(|V|)O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|) Fib heapO(|V|)O(|V| log |V|) O(|E|) O(|V| log |V| + |E|) O(|E| log |V|)

What about Dijkstra’s on…? A B C E D

What about Dijkstra’s on…? A B C E D

What about Dijkstra’s on…? A B C E D Dijkstra’s algorithm only works for positive edge weights