Graphs + Shortest Paths David Kauchak cs302 Spring 2013.

Slides:



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

Graph Traversals and Minimum Spanning Trees. Graph Terminology.
Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
Graphs – Depth First Search ORD DFW SFO LAX
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.
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
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.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
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?
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Tirgul 11 DFS Properties of DFS Topological sort.
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Dan Grossman Spring 2010.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
Graph Traversals and Minimum Spanning Trees : Fundamental Data Structures and Algorithms Rose Hoberman April 8, 2003.
Lecture 10 Topics Application of DFS Topological Sort
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
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.
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.
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed.
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.
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
CSC 331: Algorithm Analysis Decompositions of Graphs.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Representing and Using Graphs
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.
Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010.
ADA: 9. Graph Search1 Objective o describe and compare depth-first and breadth- first graph searching, and look at the creation of spanning trees.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
7. Graph Traversal Describe and compare depth-first and breadth-first graph searching, and look at the creation of spanning trees Contest Algorithms: 7.
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
GRAPH ALGORITHM. Graph A pair G = (V,E) – V = set of vertices (node) – E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ( (1,2),(2,3),(3,5),(1,4),(4,5),(6,7)
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.
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.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
Chapter 22 Elementary Graph Algorithms
CSE 373 Topological Sort Graph Traversals
CSE 2331/5331 Topic 9: Basic Graph Alg.
Topological Sort Minimum Spanning Tree
CSC 413/513: Intro to Algorithms
Many slides here are based on E. Demaine , D. Luebke slides
CS 3343: Analysis of Algorithms
Graph Representation Adjacency list representation of G = (V, E)
Advanced Algorithms Analysis and Design
Search Related Algorithms
Chapter 22: Elementary Graph Algorithms III
Presentation transcript:

Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Admin

Tree BFS

Running time of Tree BFS Adjacency list How many times does it visit each vertex? How many times is each edge traversed? O(|V|+|E|) Adjacency matrix For each vertex visited, how much work is done? O(|V| 2 )

BFS Recursively Hard to do!

BFS for graphs What needs to change for graphs? Need to make sure we don’t visit a node multiple times B DE F A C G

distance variable keeps track of how far from the starting node and whether we’ve seen the node yet B DE F A C G

B DE F A C G

set all nodes as unseen B DE F A C G

check if the node has been seen B DE F A C G

set the node as seen and record distance B DE F A C G

B DE F A C G      

B DE F A C G 0      Q: A

B DE F A C G 0      Q:

B DE F A C G 0 1   1 1 Q: D, E, B

B DE F A C G 0 1   1 1 Q: E, B

B DE F A C G 0 1   1 1 Q: B

B DE F A C G 0 1   1 1

B DE F A C G 0 1   1 1 Q:

B DE F A C G 0 1   1 1

B DE F A C G  1 1 Q: F, C

B DE F A C G

B DE F A C G

B DE F A C G

Is BFS correct? Does it visit all nodes reachable from the starting node? Can you prove it? Assume we “miss” some node ‘u’, i.e. a path exists, but we don’t visit ‘u’ S … U

Is BFS correct? Does it visit all nodes reachable from the starting node? Can you prove it? Find the last node along the path to ‘u’ that was visited S … U … ZW why do we know that such a node exists?

Is BFS correct? Does it visit all nodes reachable from the starting node? Can you prove it? We visited ‘z’ but not ‘w’, which is a contradiction, given the pseudocode S … U … ZW contradiction

Is BFS correct? Does it correctly label each node with the shortest distance from the starting node?

Is BFS correct? Does it correctly label each node with the shortest distance from the starting node? Assume the algorithm labels a node with a longer distance. Call that node ‘u’ S … U

Is BFS correct? Does it correctly label each node with the shortest distance from the starting node? Find the last node in the path with the correct distance S … U … ZW correct incorrect

Is BFS correct? Does it correctly label each node with the shortest distance from the starting node? Find the last node in the path with the correct distance S … U … ZW contradiction

Runtime of BFS Nothing changed over our analysis of TreeBFS

Runtime of BFS Adjacency list: O(|V| + |E|) Adjacency matrix: O(|V| 2 )

Depth First Search (DFS)

Tree DFS A B C E D FG

A B C E D FG

A B C E D FG

A B C E D FG

A B C E D FG Frontier?

Tree DFS A B C E D FG

A B C E D FG

A B C E D FG

A B C E D FG

DFS on graphs

mark all nodes as not visited

DFS on graphs until all nodes have been visited repeatedly call DFS-Visit

DFS on graphs What happened to the stack?

What does DFS do? Finds connected components Each call to DFS-Visit from DFS starts exploring a new set of connected components Helps us understand the structure/connectedness of a graph

Is DFS correct? Does DFS visit all of the nodes in a graph?

Running time? Like BFS Visits each node exactly once Processes each edge exactly twice (for an undirected graph) O(|V|+|E|)

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

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