Nattee Niparnan. Distance of nodes  The distance between two nodes is the length of the shortest path between them S  A 1 S  C 1 S  B 2.

Slides:



Advertisements
Similar presentations
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Advertisements

Single Source Shortest Paths
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph A graph, G = (V, E), is a data structure where: V is a set of vertices (aka nodes) E is a set of edges We use graphs to represent relationships among.
CSE 101- Winter ‘15 Discussion Section January 26th 2015.
Breadth-First and Depth-First Search
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.
1 Paths in Graphs Oscar Miguel Alonso M. 2 Outline The problem to be solved Breadth first search Dijkstra's Algorithm Bellman-Ford Algorithm Shortest.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
#1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching.
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.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
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.
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.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Shortest Path Algorithms
Shortest Paths Definitions Single Source Algorithms
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
DAST 2005 Tirgul 12 (and more) sample questions. DAST 2005 Q.We’ve seen that solving the shortest paths problem requires O(VE) time using the Belman-Ford.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
All-Pairs Shortest Paths
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
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.
Dijkstra's algorithm.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
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.
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
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.
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.
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,
Graphs, BFS, DFS and More…
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
Graphs Data Structures and Algorithms A. G. Malamos Reference Algorithms, 2006, S. Dasgupta, C. H. Papadimitriou, and U. V. Vazirani Introduction to Algorithms,Third.
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.
Dijkstra’s Algorithm Supervisor: Dr.Franek Ritu Kamboj
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.
Nattee Niparnan. Dijkstra’s Algorithm Graph with Length.
Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun.
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
Shortest Paths PowerPoint adapted from Alan Tam’s Shortest Path, 2004 with slight modifications.
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.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
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)
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
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.
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,
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Graphs.
Spanning Trees Longin Jan Latecki Temple University based on slides by
BFS,DFS Topological Sort
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Chapter 11 Graphs.
Algorithms Searching in a Graph.
Lecture 12 Shortest Path.
Graphs: Shortest path and mst
Presentation transcript:

Nattee Niparnan

Distance of nodes  The distance between two nodes is the length of the shortest path between them S  A 1 S  C 1 S  B 2

DFS and Length  DFS finds all nodes reachable from the starting node  But it might not be “visited” according to the distance

Ball and Strings We can compute distance by proceeding from “layer” to “layer”

Shortest Path Problem (Undi, Unit)  Input:  A graph, undirected  A starting node S  Output:  A label on every node, giving the distance from S to that node

Breadth-First-Search  Visit node according to its layer procedure bfs(G; s) //Input: Graph G = (V,E), directed or undirected; vertex s  V //Output: visit[u] is set to true for all nodes u reachable from v for all u  V : visit[u] = false visit[s] = true Queue Q = [s] (queue containing just s) while Q is not empty: u = eject(Q) previsit(u) visit[u] = true; for all edges (u,v)  E: if visit[v] = false: visit[v] = true; inject(Q,v); postvisit(u)

Distance using BFS procedure shortest_bfs(G, s) //Input: Graph G = (V,E), directed or undirected; vertex s  V //Output: For all vertices u reachable from s, dist(u) is set to the distance from s to u. for all u  V : dist[u] = -1 dist[s] = 0 Queue Q = [s] (queue containing just s) while Q is not empty: u = eject(Q); for all edges (u,v)  E: if dist[v] = -1: inject(Q,v); dist[v] = dist[u] + 1; Use dist as visit

DFS by Stack procedure dfs(G, s) //Input: Graph G = (V,E), directed or undirected; vertex s  V //Output: visit[u] is set to true for all nodes u reachable from v for all u  V : visit[u] = false visit[s] = true Stack S = [s] (queue containing just s) while S is not empty: u = pop(S) previsit(u) for all edges (u,v)  E: if visit [v] = false: push(S,v) visit[v] = true; postvisit(u)

DFS vs BFS  DFS goes depth first  Trying to go further if possible  Backtrack only when no other possible way to go  Using Stack  BFS goes breadth first  Trying to visit node by the distance from the starting node  Using Queue

Dijkstra’s Algorithm Graph with Length

Edge with Length Length function l(a,b) = distance from a to b

Finding Shortest Path  BFS can give us the shortest path  Just convert the length edge into unit edge However, this is very slow Imagine a case when the length is 1,000,000

Alarm Clock Analogy  No need to walk to every node  Since it won’t change anything  We skip to the “actual” node  Set up the clock at alarm at the target node

Alarm Clock Algorithm  Set an alarm clock for node s at time 0.  Repeat until there are no more alarms:  Say the next alarm goes off at time T, for node u. Then:  The distance from s to u is T.  For each neighbor v of u in G:  If there is no alarm yet for v, set one for time T + l(u, v).  If v's alarm is set for later than T + l(u, v), then reset it to this earlier time.

Dijkstra’s Algo from BFS procedure dijkstra(G, l, s) //Input: Graph G = (V;E), directed or undirected; vertex s  V; positive edge lengths l // Output: For all vertices u reachable from s, dist[u] is set to the distance from s to u. for all u  V : dist[u] = + prev(u) = nil dist[s] = 0 H = makequeue(V) (using dist-values as keys) while H is not empty: u = deletemin(H) for all edges (u; v)  E: if dist[v] > dist[u] + l(u, v): dist[v] = dist[u] + l(u, v) prev[v] = u decreasekey(H, v)

Another Implementation of Dijkstra’s  Growing from Known Region of shortest path  Given a graph and a starting node s  What if we know a shortest path from s to some subset S’  V?  Divide and Conquer Approach?

Dijktra’s Algo #2 procedure dijkstra(G, l, s) //Input: Graph G = (V;E), directed or undirected; vertex s  V; positive edge lengths l // Output: For all vertices u reachable from s, dist[u] is set to the distance from s to u. for all u  V : dist[u] = + prev(u) = nil dist[s] = 0 R = {} // (the “known region”) while R ≠ V : Pick the node v  R with smallest dist[] Add v to R for all edges (v,z)  E: if dist[z] > dist[v] + l(v,z): dist[z] = dist[v] + l(v,z)

Analysis  There are |V| ExtractMin  Need to check all edges  At most |E|, if we use adjacency list  Maybe |V 2 |, if we use adjacency matrix  Value of dist[] might be changed  Depends on underlying data structure

Choice of DS  Using simple array  Each ExtractMin uses O(V)  Each change of dist[] uses O(1)  Result = O(V 2 + E) = O(V 2 )  Using binary heap  Each ExtractMin uses O(lg V)  Each change of dist[] uses O(lg V)  Result = O( (V + E) lg V)  Can be O (V 2 lg V) Might be V 2 Good when the graph is sparse

Fibonacci Heap  Using simple array  Each ExtractMin uses O( lg V) (amortized)  Each change of dist[] uses O(1) (amortized)  Result = O(V lg V + E)

Graph with Negative Edge  Disjktra’s works because a shortest path to v must pass throught a node closer than v  Shortest path to A pass through B which is… in BFS sense… is further than A

Negative Cycle  A graph with a negative cycle has no shortest path  The shortest.. makes no sense..  Hence, negative edge must be a directed

Key Idea in Shortest Path  Update the distance if dist[z] > dist[v] + l(v,z): dist[z] = dist[v] + l(v,z)  This is safe to perform  now, a shortest path must has at most |V| - 1 edges

Bellman-Ford Algorithm procedure BellmanFord(G, l, s) //Input: Graph G = (V;E), directed; vertex s  V; edge lengths l (may be negative), no negative cycle // Output: For all vertices u reachable from s, dist[u] is set to the distance from s to u. for all u  V : dist[u] = + prev(u) = nil dist[s] = 0 repeat |V| - 1 times: for all edges (a,b)  E: if dist[b] > dist[a] + l(a,b): dist[b] = dist[a] + l(a,b)

Shortest Path in DAG  Path in DAG appears in linearized order procedure dag-shortest-path(G, l, s) //Input: DAG G = (V;E), vertex s  V; edge lengths l (may be negative) // Output: For all vertices u reachable from s, dist[u] is set to the distance from s to u. for all u  V : dist[u] = + prev(u) = nil dist[s] = 0 Linearize G For each u  V, in linearized order: for all edges (u,v)  E: if dist[v] > dist[u] + l(u,v): dist[v] = dist[u] + l(y,v)