Graphs, BFS, DFS and More…

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
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.
Introduction to Algorithms 6.046J/18.401J/SMA5503
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.
Graph & BFS.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
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.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lectures 3 Tuesday, 9/25/01 Graph Algorithms: Part 1 Shortest.
Shortest Path Problems
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2008 Lecture 4 Tuesday, 9/30/08 Graph Algorithms: Part 1 Shortest.
1 Data Structures DFS, Topological Sort Dana Shapira.
Lecture 10 Topics Application of DFS Topological Sort
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
Graph Algorithms: Part 1
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Lecture 10 Graph Algorithms. Definitions Graph is a set of vertices V, with edges connecting some of the vertices (edge set E). An edge can connect two.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
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.
How to navigate a maze? Can we go from s to t? Shortest route? How fast can we compute this?
Graph Algorithms Introduction to Algorithms Graph Algorithms CSE 680 Prof. Roger Crawfis Partially from io.uwinnipeg.ca/~ychen2.
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.
SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the.
November 6, Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
COSC 3101A - Design and Analysis of Algorithms 10
Elementary Graph Algorithms CSc 4520/6520 Fall 2013 Slides adapted from David Luebke, University of Virginia and David Plaisted, University of North Carolina.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Introduction to Algorithms Jiafen Liu Sept
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.
Combinatorics Shortest path Maximum flow & minimum cut
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Elementary Graph Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
Lecture 13 Algorithm Analysis
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Graph. Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices.
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.
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
Chapter 05 Introduction to Graph And Search Algorithms.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
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,
CS138A Elementary Graph Algorithms Peter Schröder.
Elementary Graph Algorithms
Graph Representation Adjacency list representation of G = (V, E)
Advanced Algorithms Analysis and Design
Chapter 11 Graphs.
Lecture 13 Algorithm Analysis
CE 221 Data Structures and Algorithms
CE 221 Data Structures and Algorithms
Elementary Graph Algorithms
Advanced Algorithms Analysis and Design
Presentation transcript:

Graphs, BFS, DFS and More… ctlDenon

Agenda Review More Graphs Graphs Representations DFS BFS Topological-Sort Shortest Path Dijkstra’s , Bellman-Ford All Pairs Shortest Path Dijkstra’s, Floyd-WarShall

Graphs

Graphs (review) Definition. A directed graph (digraph) G = (V, E) is an ordered pair consisting of a set V of vertices (singular: vertex) a set E ⊆ V × V of edges In an undirected graph G = (V, E), the edge set E consists of unordered pairs of vertices. More, Connected, Disconnected Tree, Forest Cyclic, Acyclic Weighted, un-weighted Multigraph …

Graphs Representations - adjacency matrix

Graphs Representations - adjacency matrix The adjacency matrix of a graph G = (V, E), where V = {1, 2, …, n}, is the matrix A[1 . . n, 1 . . n] given by A[i, j] = 1 if (i, j) ∈ E, (What if multigraph ???) 0 if (i, j) ∉ E. Storage size??

Graphs Representations - adjacency list An adjacency list of a vertex v ∈ V is the list Adj[v] of vertices adjacent to v. Storage size??

DFS and BFS Review Possible ordering of traversing if DFS? Possible ordering of traversing if BFS?

BFS BFS(G) marks all Vertices in G as un-visited marks start vertex s as visited enqueue s into queue Q while queue not empty dequeue the first vertex u from queue Q for each vertex v directly reachable from u if v is unvisited enqueue v to queue Q mark v as visited

BFS Application while queue not empty Shortest Path of un-weighted while queue not empty dequeue the first vertex u from queue for each neighbor vertex v of u if v is unvisited enqueue v to queue mark v as visited parent[v] = u

DFS DFS(G) for each vertex u in G color[u] = WHITE if color[u] = WHITE DFS-visit(u) DFS-visit(u) color[u] = GRAY for each neighbor vertex v of u if color[v] = WHITE DFS-visit[v]

Topological Sort Linear ordering of a directed acyclic graph (DAG)’ s nodes in which each node comes before all nodes to which it has outbound edges.

Topological Sort DFS(G) for each vertex u in G color[u] = WHITE parent[u] = Nil time = 0 if color[u] = WHITE DFS-visit(u) DFS-visit(u) color[u] = GRAY for each neighbor vertex v of u if color[v] = WHITE parent[v] = u DFS-visit[v] color[u] = BLACK time = time + 1 f[u] = time

Topological Sort Call DFS(G) to compute the finishing times f[v] for each vertex v As each vertex is finished (Mark as Black), insert it onto the front of a list Return the list

Shortest Path

Shortest Path More Formal Definition Consider a digraph G = (V, E) with edge-weight function w : E → R. The weight of path p = v1 → v2 → … → vk is defined to be A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as (u, v) = min{w(p) : p is a path from u to v}. Note: δ(u, v) = ∞ if no path from u to v exists.

Shortest Path Optimal substructure Theorem. A subpath of a shortest path is a shortest path.

Shortest Path Triangle inequality Theorem. For all u, v, x ∈ V, we have δ(u, v) ≤δ(u, x) + δ(x, v). Relaxation: if d[v] > d[x] + w(x, v) then d[v] ← d[x] + w(x, v)

Shortest Path Un-weighted graph?

Shortest Path Un-weighted graph? BFS

Shortest Path Un-weighted graph? BFS Non-negative edge weights?

Shortest Path Un-weighted graph? BFS Non-negative edge weights? Dijkstra’s algorithm

Shortest Path – Dijkstra’s algorithm Suppose you create a knotted web of strings, with each knot corresponding to a node, and the strings corresponding to the edges of the web: the length of each string is proportional to the weight of each edge. Now you compress the web into a small pile without making any knots or tangles in it. You then grab your starting knot and pull straight up. As new knots start to come up with the original, you can measure the straight up-down distance to these knots: this must be the shortest distance from the starting node to the destination node. The acts of "pulling up" and "measuring" must be abstracted for the computer, but the general idea of the algorithm is the same: you have two sets, one of knots that are on the table, and another of knots that are in the air. Every step of the algorithm, you take the closest knot from the table and pull it into the air, and mark it with its length. If any knots are left on the table when you're done, you mark them with the distance infinity.

Shortest Path – Dijkstra’s algorithm

Shortest Path – Dijkstra’s algorithm

Shortest Path – Dijkstra’s algorithm

Shortest Path – Dijkstra’s algorithm

Shortest Path – Dijkstra’s algorithm

Shortest Path – Dijkstra’s algorithm

Shortest Path – Dijkstra’s algorithm

Shortest Path – Dijkstra’s algorithm

Shortest Path – Dijkstra’s algorithm

Shortest Path – Dijkstra’s algorithm

Shortest Path – Dijkstra’s algorithm

Shortest Path – Dijkstra’s algorithm

Shortest Path – Dijkstra’s algorithm int dist[N]; int pred[N]; struct C{ int u,c; C(){} C(int a,int b):u(a),c(b){} bool operator<(const C&a) const { return c > a.c; } }; // the graph is represented by // a adjacency list // a vertex a has adjc[a] neighbors // adj[a][0] … adj[a][adjc[a]-1] are // neighbors of vertex // cost[a][b] has edge cost for (a,b) int adjc[N]; int adj[N][N]; int cost[N][N]; bool visit[N]; void dijkstra(int s){ memset(visit,0,sizeof(visit)); memset(dist,0x7f,sizeof(dist)); priority_queue<C> pq; pq.push(C(s,0)); dist[s] = 0; while(pq.size()>0){ C c = pq.top(); pq.pop(); int u = c.u; if(visit[u]) continue; visit[u] = true; for(int i=0;i<adjc[u];++i){ int v = adj[u][i]; if(visit[v]) continue; if(c.c+cost[u][v]<dist[v]){ dist[v] = dist[u] + cost[u][v]; pred[v] = u; pq.push(C(v, dist[v])); }

Shortest Path Un-weighted graph? BFS Non-negative edge weights? Dijkstra’s algorithm

Shortest Path Un-weighted graph? BFS Non-negative edge weights? Dijkstra’s algorithm Negative edge weights?

Shortest Path Un-weighted graph? BFS Non-negative edge weights? Dijkstra’s algorithm Negative edge weights? Bellman-Ford algorithm

Shortest Path – Bellman Ford If a graph G = (V, E) contains a negative- weight cycle, then some shortest paths may not exist.

Shortest Path – Bellman Ford Bellman-Ford algorithm: Finds all shortest-path lengths from a sources∈V to all v∈V or determines that a negative-weight cycle exists.

Shortest Path – Bellman Ford

Shortest Path – Bellman Ford

Shortest Path – Bellman Ford

Shortest Path – Bellman Ford

Shortest Path – Bellman Ford

Shortest Path – Bellman Ford

Shortest Path – Bellman Ford

Shortest Path – Bellman Ford

Shortest Path – Bellman Ford

Shortest Path – Bellman Ford

Shortest Path – Bellman Ford

Shortest Path – Bellman Ford

Shortest Path – Bellman Ford

Shortest Path – Bellman Ford

Shortest Path – Bellman Ford

Shortest Path – Bellman Ford

Shortest Path – Bellman Ford

Shortest Path – Bellman Ford

Shortest Path – Bellman Ford

Shortest Path – Bellman Ford

Shortest Path – Bellman Ford

Shortest Path – Bellman Ford

Shortest Path – Bellman Ford Theorem. If G= (V, E)contains no negative-weight cycles, then after the Bellman-Ford algorithm executes, d[v] = δ(s, v)for all v∈V. Proof. Let v∈V be any vertex, and consider a shortest path p from s to v with the minimum number of edges. Since p is a shortest path, we have δ(s, vi) = δ(s, vi–1) + w(vi–1, vi) .

Shortest Path – Bellman Ford If G contains no negative-weight cycles, p is simple. Longest simple path has ≤|V|–1edges. Initially, d[v0] = 0 = δ(s, v0), and d[v0] is unchanged by subsequent relaxations (because of the lemma from Lecture 14 that d[v] ≥δ(s, v)). After 1pass through E, we have d[v1] = δ(s, v1). After 2passes through E, we have d[v2] = δ(s, v2).M After k - passes through E, we have d[vk] = δ(s, vk).

Shortest Path – Bellman Ford If a value d[v] fails to converge (stop changing) after |V|–1passes, there exists a negative-weight cycle in G reachable from s

Shortest Path Un-weighted graph? BFS Non-negative edge weights? Dijkstra’s algorithm Negative edge weights? Bellman-Ford algorithm DAG?

Shortest Path Un-weighted graph? BFS Non-negative edge weights? Dijkstra’s algorithm Negative edge weights? Bellman-Ford algorithm DAG? Topological Sort + One Round of Bellman-Ford

Shortest Path Un-weighted graph? BFS Non-negative edge weights? Dijkstra’s algorithm Negative edge weights? Bellman-Ford algorithm DAG? Topological Sort + One Round of Bellman-Ford All Pairs Shortest Path?

Shortest Path Un-weighted graph? BFS Non-negative edge weights? Dijkstra’s algorithm Negative edge weights? Bellman-Ford algorithm DAG? Topological Sort + One Round of Bellman-Ford All Pairs Shortest Path?

All Pairs Shortest Path Non-negative edge weights

All Pairs Shortest Path Non-negative edge weights Dijkstra’s algorithm |V| times

All Pairs Shortest Path Non-negative edge weights Dijkstra’s algorithm |V| times Negative edge

All Pairs Shortest Path Non-negative edge weights Dijkstra’s algorithm |V| times Negative edge Floyd-WarShall (Not the fastest one, but good enough in most cases)

All Pairs Shortest Path - Floyd-WarShall Define Cij(k)=weight of a shortest path from I to j with intermediate vertices belonging to the set {1, 2, …, k}. Thus, δ(i, j) = Cij(n). Also, Cij(0) = w(i, j) .

All Pairs Shortest Path - Floyd-WarShall Cij(k)= min { Cij(k–1),Cik(k–1)+ C kj(k–1) } intermediate vertices in {1, 2, …, k}

All Pairs Shortest Path - Floyd-WarShall

Reference Depth-First Search http://en.wikipedia.org/wiki/Depth-first_search Graph Algorithms http://i.cs.hku.hk/~provinci/training07/02-Graph_Theory.ppt Topological Sorting http://en.wikipedia.org/wiki/Topological_sorting Greedy Algorithms (and Graphs) http://velblod.videolectures.net/2005/ocw/mit/6.046j/mit6046jf05_leiserson_lec16/mit6046jf05_leiserson_lec16_01.pdf Shortest Paths I http://carbon.videolectures.net/2005/ocw/mit/6.046j/mit6046jf05_demaine_lec17/mit6046jf05_demaine_lec17_01.pdf Shortest Paths II http://carbon.videolectures.net/2005/ocw/mit/6.046j/mit6046jf05_demaine_lec18/mit6046jf05_demaine_lec18_01.pdf Shortest Paths III http://carbon.videolectures.net/2005/ocw/mit/6.046j/mit6046jf05_demaine_lec18/mit6046jf05_demaine_lec19_01.pdf