CS 6301 PROGRAMMING AND DATA STRUCTURE II Dept of CSE/IT UNIT V GRAPHS

Similar documents
Discrete Wiskunde II. Lecture 5: Shortest Paths & Spanning Trees

Analysis of Algorithms. Outline. Single Source Shortest Path. Andres Mendez-Vazquez. November 9, Notes. Notes

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

Single Source Shortest Paths

Slides credited from Hsueh-I Lu & Hsu-Chun Hsiao

CS 4407 Algorithms Lecture: Shortest Path Algorithms

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

Design and Analysis of Algorithms

Algorithms Booklet. 1 Graph Searching. 1.1 Breadth First Search

Graph Search Howie Choset

Design and Analysis of Algorithms

Shortest Path Algorithms

Shortest Paths. CS 320, Fall Dr. Geri Georg, Instructor 320 ShortestPaths 3

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

CS 410/584, Algorithm Design & Analysis, Lecture Notes 4

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

BFS Dijkstra. Oct abhi shelat

Ngày 20 tháng 7 năm Discrete Optimization Graphs

Data Structures and and Algorithm Xiaoqing Zheng

IS 2610: Data Structures

Single Source Shortest Paths

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Query Processing in Spatial Network Databases

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Preliminaries. Graphs. E : set of edges (arcs) (Undirected) Graph : (i, j) = (j, i) (edges) V = {1, 2, 3, 4, 5}, E = {(1, 3), (3, 2), (2, 4)}

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

ICS 252 Introduction to Computer Design

Introduction to Algorithms

CSE 431/531: Analysis of Algorithms. Dynamic Programming. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

CMPS 2200 Fall Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk. 10/8/12 CMPS 2200 Intro.

Algorithm Design and Analysis

Trees. A tree is a graph which is. (a) Connected and. (b) has no cycles (acyclic).

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

Lecture 11. Single-Source Shortest Paths All-Pairs Shortest Paths

IS 709/809: Computational Methods in IS Research Fall Exam Review

FINAL EXAM PRACTICE PROBLEMS CMSC 451 (Spring 2016)

Algorithm Design Strategies V

Queues. Principles of Computer Science II. Basic features of Queues

ne a priority queue for the nodes of G; ile (! PQ.is empty( )) select u PQ with d(u) minimal and remove it; I Informatik 1 Kurt Mehlhorn

CS 410/584, Algorithm Design & Analysis: Lecture Notes 3

Divide-and-Conquer Algorithms Part Two

Introduction to Algorithms

GRAPH ALGORITHMS Week 3 (16-21 October 2017)

Introduction to Algorithms

Breadth-First Search of Graphs

Solution suggestions for examination of Logic, Algorithms and Data Structures,

Introduction to Algorithms

CS 161: Design and Analysis of Algorithms

Contents Lecture 4. Greedy graph algorithms Dijkstra s algorithm Prim s algorithm Kruskal s algorithm Union-find data structure with path compression

Algorithms and Data Structures (COMP 251) Midterm Solutions

Part V. Matchings. Matching. 19 Augmenting Paths for Matchings. 18 Bipartite Matching via Flows

Algorithms: Lecture 12. Chalmers University of Technology

Directed Graphs (Digraphs) and Graphs

Algorithm Design and Analysis

Discrete Optimization 2010 Lecture 3 Maximum Flows

University of Toronto Department of Electrical and Computer Engineering. Final Examination. ECE 345 Algorithms and Data Structures Fall 2016

Chapter 9: Relations Relations

Fundamental Algorithms 11

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

Maximum Flow Problem (Ford and Fulkerson, 1956)

SI545 VLSI CAD: Logic to Layout. Algorithms

A Simple Implementation Technique for Priority Search Queues

CS213d Data Structures and Algorithms

CSC 1700 Analysis of Algorithms: Warshall s and Floyd s algorithms

Informatique Fondamentale IMA S8

Solutions. Prelim 2[Solutions]

CSE613: Parallel Programming, Spring 2012 Date: May 11. Final Exam. ( 11:15 AM 1:45 PM : 150 Minutes )

Informed Search. Chap. 4. Breadth First. O(Min(N,B L )) BFS. Search. have same cost BIBFS. Bi- Direction. O(Min(N,2B L/2 )) BFS. have same cost UCS

Greedy Alg: Huffman abhi shelat

Algorithm Design CS 515 Fall 2015 Sample Final Exam Solutions

CS 161: Design and Analysis of Algorithms

CSE 4502/5717 Big Data Analytics Spring 2018; Homework 1 Solutions

Lecture Notes for Chapter 25: All-Pairs Shortest Paths

6.889 Lecture 4: Single-Source Shortest Paths

Circuits. CSE 373 Data Structures

CS 253: Algorithms. Chapter 24. Shortest Paths. Credit: Dr. George Bebis

Discrete Mathematics, Spring 2004 Homework 9 Sample Solutions

Myriad of applications

Optimal Tree-decomposition Balancing and Reachability on Low Treewidth Graphs

Computational Boolean Algebra. Pingqiang Zhou ShanghaiTech University

A faster algorithm for the single source shortest path problem with few distinct positive lengths

Lecture 7: Shortest Paths in Graphs with Negative Arc Lengths. Reading: AM&O Chapter 5

More on NP and Reductions

All-Pairs Shortest Paths

Analysis of Algorithms I: All-Pairs Shortest Paths

AC64/AT64/ AC115/AT115 DESIGN & ANALYSIS OF ALGORITHMS

Examination paper for TDT4120 Algorithms and Data Structures

Routing Algorithms. CS60002: Distributed Systems. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Informed Search. Day 3 of Search. Chap. 4, Russel & Norvig. Material in part from

Algorithm Design and Analysis

CS675: Convex and Combinatorial Optimization Fall 2016 Combinatorial Problems as Linear and Convex Programs. Instructor: Shaddin Dughmi

Priority queues implemented via heaps

Part IA Algorithms Notes

Partha Sarathi Mandal

Matching Residents to Hospitals

Data Structures in Java

CS781 Lecture 3 January 27, 2011

Data Structures in Java

Steps towards Decentralized Deterministic Network Coding

MAKING A BINARY HEAP

Transcription:

UNIT V GRAPHS Representation of Graphs Breadth-first search Depth-first search Topological sort Minimum Spanning Trees Kruskal and Prim algorithm Shortest path algorithm Dijkstra s algorithm Bellman-Ford algorithm Floyd - Warshall algorithm. Two ways of representing a graph are: a. Adjacency matrix b. Adjacency list Graph Traversals 1. BFS 2. DFS St.Joseph s College of Engineering/St.Joseph s Institute of Technology 1

DFS follows the following rules: 1. Select an unvisited node x, visit it, and treat as the current node 2. Find an unvisited neighbor of the current node, visit it, and make it the new current node; 3. If the current node has no unvisited neighbors, backtrack to the its parent, and make that parent the new current node; 4. Repeat steps 3 and 4 until no more nodes can be visited. 5. If there are still unvisited nodes, repeat from step 1. DFS(input: Graph G) { Stack S; Integer x, t; while (G has an unvisited node x){ visit(x); push(x,s); while (S is not empty){ t := peek(s); if (t has an unvisited neighbor y){ visit(y); push(y,s); else pop(s); BFS follows the following rules: 1. Select an unvisited node x, visit it, have it be the root in a BFS tree being formed. Its level is called the current level. 2. From each node z in the current level, in the order in which the level nodes were visited, visit all the unvisited neighbors of z. The newly visited nodes from this level form a new level that becomes the next current level. 3. Repeat step 2 until no more nodes can be visited. 4. If there are still unvisited nodes, repeat from Step 1. BFS(input: graph G) { Queue Q; Integer x, z, y; while (G has an unvisited node x) { visit(x); Enqueue(x,Q); while (Q is not empty){ z := Dequeue(Q); for all (unvisited neighbor y of z){ visit(y); Enqueue(y,Q); St.Joseph s College of Engineering/St.Joseph s Institute of Technology 2

Explain topological sort with suitable algorithm and example (16) Definition: A topological sort is a linear ordering of vertices in a directed acyclic graph such that if there is a path from Vi to Vj, then Vj appears after Vi in the linear ordering. Topological ordering is not possible. If the graph has a cycle, since for two vertices v and w on the cycle, v precedes w and w precedes v. To implement the topological sort, perform the following steps. Step 1 : - Find the indegree for every vertex. Step 2 : - Place the vertices whose indegree is `0' on the empty queue. Step 3 : - Dequeue the vertex V and decrement the indegree's of all its adjacent vertices. Step 4 : - Enqueue the vertex on the queue, if its indegree falls to zero. Step 5 : - Repeat from step 3 until the queue becomes empty. Step 6 : - The topological ordering is the order in which the vertices dequeued. Routine to perform Topological Sort void topsort (graph g) { queue q ; int counter = 0; vertex v, w ; q = createqueue (numvertex); makeempty (q); for each vertex v if (indegree [v] = = 0) enqueue (v, q); St.Joseph s College of Engineering/St.Joseph s Institute of Technology 3

while (! isempty (q)) { v = dequeue (q); topnum [v] = + + counter; for each w adjacent to v if (--indegree [w] = = 0) enqueue (w, q); if (counter! = numvertex) error (" graph has a cycle"); disposequeue (q); /* free the memory */ Example 1: A B E C D Vertex In degree before Dequeue 5 A 0 0 0 0 0 B 1 0 0 0 0 C 2 1 1 0 0 D 2 1 0 0 0 E 2 2 1 0 0 Enqueue A B D C, E Dequeue A B D C E Dequeue A B D E C Example 2: Error St.Joseph s College of Engineering/St.Joseph s Institute of Technology 4

Example 3: A B C D E Vertex In degree before Dequeue 5 A 0 0 0 0 0 B 1 0 0 0 0 C 1 0 0 0 0 D 3 2 1 0 0 E 2 2 1 1 0 Enqueue A B, D E C Dequeue A B C D E Dequeue A C B D E Minimum Spanning Tree Algorithm: i. Prim s algorithm ii. Kruskals algorithm 1. Write and explain the prim s algorithm with an example (16) (MAY/JUNE 2012) (NOV/DEC 2011) Prim's Algorithm to Construct a Minimal Spanning Tree Input: A weighted, connected and undirected graph G = (V,E). Output: A minimal spanning tree of G. Step 1: Let x be any vertex in V. Let X = and Y = V Step 2: Select an edge (u,v) from E such that, and (u,v)has the smallest weight among edges between X and Y. Step 3: Connect u to v. Step 4: If Y is empty, terminate and the resulting tree is a minimal spanning tree. Otherwise, go to Step 2. Initialization a. Pick a vertex r to be the root b. Set D(r) = 0, parent(r) = null c. For all vertices v V, v r, set D(v) = d. Insert all vertices into priority queue P, using distances as the keys While P is not empty { 1. Select the next vertex u to add to the tree u = P.deleteMin() St.Joseph s College of Engineering/St.Joseph s Institute of Technology 5

P) 2. Update the weight of each vertex w adjacent to u which is not in the tree (i.e., w If weight(u,w) < D(w), a. parent(w) = u b. D(w) = weight(u,w) c. Update the priority queue to reflect new distance for w 2. Explain Kruskal s algorithm with an example (16) Kruskal's Algorithm to Construct a Minimal Spanning Tree Input: A weighted, connected and undirected graph G = (V,E). Output: A minimal spanning tree of G. Step 1: T = Step 2: while T contains less than n-1edges do Choose an edge (v,w) from E of the smallest weight. Delete (v,w) from E. If the adding of (v,w)does not create cycle in T then Add (v,w) to T. Else Discard (v,w). end while A B C D E Edge Weight Action Comments (A,F) 10 Accepted An edge with minimum cost(from delete_min of heap (C,D) 12 Accepted (B,G) 14 Accepted (B,C) 16 Accepted (D,E) 22 Accepted (E,G) 24 Rejected Forms a cycle (F,E) 25 Accepted (A,B) 28 Rejected Forms a cycle St.Joseph s College of Engineering/St.Joseph s Institute of Technology 6

1. Single-source shortest paths in weighted graphs i. Dijkstra s Algorithm ii. Bellman-Ford Algorithm 2. All pair shortest path algorithm a. Floyd Warshall Algorithm Dijkstra(G,s) 01 for each vertex u Î G.V() 02 u.setd( ) 03 u.setparent(nil) 04 s.setd(0) 05 S Æ 06 Q.init(G.V()) 07 while not Q.isEmpty() 08 u Q.extractMin() 09 S S È {u 10 for each v Î u.adjacent() do 11 Relax(u, v, G) 12 Q.modifyKey(v) 13 Relax (u,v,g) 14 if v.d() > u.d()+g.w(u,v) then 15 v.setd(u.d()+g.w(u,v)) 16 v.setparent(u) Dijkstra s Algorithm St.Joseph s College of Engineering/St.Joseph s Institute of Technology 7

St.Joseph s College of Engineering/St.Joseph s Institute of Technology 8

Initial matrix a 0 0 - b 0 - c 0 - d 0 - e 0 - a is visited a 0 1 - b 10 0 a c 3 0 a d 0 - e 0 - c is visited a 0 1 - b 10,(3+4) 0 a,c c 3 1 a d,(3+8) 0 c e,(3+2) 0 -,c St.Joseph s College of Engineering/St.Joseph s Institute of Technology 9

e is visited a 0 1 - b 7 0 c c 3 1 a d 11,(5+9) 0 c,e e 5 1 c b is visited a 0 1 - b 7 1 c c 3,(7+1) 1 a,b d 11,(7+2) 0 c,b e 5 1 c d is visited a 0 1 - b 7 1 c c 3 1 a d 9 1 b e 5,(9+7) 1 C,d Final matrix a 0 1 - b 7 1 c c 3 1 a d 9 1 b e 5 1 c Dijkstra s doesn t work when there are negative edges: Intuition we can not be greedy any more on the assumption that the lengths of paths will only increase in the future Bellman-Ford algorithm detects negative cycles (returns false) or returns the shortest path-tree Bellman-Ford algorithm Bellman-Ford(G,s) 01 for each vertex u Î G.V() 02 u.setd( ) 03 u.setparent(nil) 04 s.setd(0) St.Joseph s College of Engineering/St.Joseph s Institute of Technology 10

05 for i 1 to G.V() -1 do 06 for each edge (u,v) Î G.E() do 07 Relax (u,v,g) 08 for each edge (u,v) Î G.E() do 09 if v.d() > u.d() + G.w(u,v) then 10 return false 11 return true 12 Relax (u,v,g) 13 if v.d() > u.d()+g.w(u,v) then 14 v.setd(u.d()+g.w(u,v)) 15 v.setparent(u) Eg.1 s 0 0 - t 0 - x 0 - y 0 - z 0 - S is Visited s 0 1 - t 6 0 s x 0 - y 7 0 s z 0 - St.Joseph s College of Engineering/St.Joseph s Institute of Technology 11

t is Visited (-ve cost) s 0 1 - t 6 1 s x 0 - y 7 0 s z (6-4) 0 t y is Visited (-ve cost) s 0 1 - t 6 1 s x,(7-3) 0 -,y y 7 1 s z 2 0 t s 0 1 - t 6 1 s x 4 0 y y 7 1 s z 2 0 t x is Visited (-ve cost) s 0 1 - t Min(6,(4-2)) 1 s,x x 4 1 y y 7 1 s z 2 0 t s 0 1 - t 2 1 x x 4 1 y y 7 1 s z 2 0 t St.Joseph s College of Engineering/St.Joseph s Institute of Technology 12

Update t s 0 1 - t 2 1 x x 4 1 y y 7 1 s z Min(2,(2-4)) 0 t The cost of moving from s to s is 0 s to t is 2 s to x is 4 s to y is 7 s to z is -2. Final matrix s 0 1 - t 2 1 x x 4 1 y y 7 1 s z -2 1 t Floyd Warshall Algorithm FloydWarshall(matrix W, integer n) for k 1 to n do for i 1 to n do for j 1 to n do d ij (k) min(d ij (k-1), d ik (k-1) + d kj (k-1) ) // D[i,j] = min(d[i,j],d[i,k]+d[k,j]) return D (n) The final D matrix will store all the shortest paths. Adjacency matrix for the given graph. 1 0-2 2 4 0 3 4-1 0 St.Joseph s College of Engineering/St.Joseph s Institute of Technology 13

K=0 1 0-2 2 4 0 3 4-1 0 K i j d[i,j] d[i,k] d[k,j] ) d[i,k]+ d[k,j] d[i,j] Min(d[i,j], d[i,k]+ d[k,j]) 1 1 1 0 0 0 0 0 0 1 1 2 0 1 1 3-2 0-2 -2-2 -2 1 1 4 0 1 2 1 4 4 0 4 4 4 1 2 2 0 4 0 0 1 2 3 3 4-2 2 3 2 1 2 4 4 1 3 1 0 1 3 2 1 3 3 0-2 0 0 1 3 4 2 2 2 1 4 1 0 1 4 2-1 -1-1 1 4 3-2 1 4 4 0 0 0 K=1 1 0-2 2 4 0 2 4-1 0 St.Joseph s College of Engineering/St.Joseph s Institute of Technology 14

K=1 1 0-2 2 4 0 2 4-1 0 k i j d[i,j] d[i,k] d[k,j] d[i,k]+ d[k,j] d[i,j] Min(d[i,j], d[i,k]+ d[k,j]) 2 1 1 0 4 0 0 2 1 2 0 2 1 3-2 2-2 -2 2 1 4 2 2 1 4 0 4 4 4 4 2 2 2 0 0 0 0 0 0 2 2 3 2 0 2 2 2 2 2 2 4 0 2 3 1 4 2 3 2 0 2 3 3 0 2 0 0 2 3 4 2 2 2 2 4 1-1 4 3 3 2 4 2-1 -1 0-1 -1-1 2 4 3-1 2 1 1 2 4 4 0-1 0 0 K=2 1 0-2 2 4 0 2 St.Joseph s College of Engineering/St.Joseph s Institute of Technology 15

K=2 1 0-2 2 4 0 2 k i j d[i,j] d[i,k] d[k,j] ) d[i,k]+ d[k,j] d[i,j] Min(d[i,j], d[i,k]+ d[k,j]) 3 1 1 0-2 0 0 3 1 2-2 3 1 3-2 -2 0-2 -2-2 3 1 4-2 2 0 0 3 2 1 4 2 4 4 3 2 2 0 2 0 0 3 2 3 2 2 0 2 2 2 3 2 4 2 2 4 4 3 3 1 0 3 3 2 0 3 3 3 0 0 0 0 0 0 3 3 4 2 0 2 2 2 2 3 4 1 3 1 3 3 3 4 2-1 1-1 -1 3 4 3 1 1 0 1 1 1 3 4 4 0 1 2 3 0 0 K=3 1 0-2 0 2 4 0 2 4 St.Joseph s College of Engineering/St.Joseph s Institute of Technology 16

K=3 1 0-2 0 2 4 0 2 4 k i j d[i,j] d[i,k] d[k,j] ) d[i,k]+ d[k,j] d[i,j] Min(d[i,j], d[i,k]+ d[k,j]) 4 1 1 0 0 3 3 0 0 4 1 2 0-1 -1-1 4 1 3-2 0 1 1-2 -2 4 1 4 0 0 0 0 0 0 4 2 1 4 4 3 7 4 4 4 2 2 0 4-1 3 0 0 4 2 3 2 4 1 5 2 2 4 2 4 4 4 0 4 4 4 4 3 1 2 3 5 5 4 3 2 2-1 1 1 4 3 3 0 2 1 3 0 0 4 3 4 2 2 0 2 2 2 4 4 1 3 0 3 3 3 3 4 4 2-1 0-1 -1-1 -1 4 4 3 1 0 1 1 1 1 4 4 4 0 0 0 0 0 0 K=4 1 0-1 -2 0 2 4 0 2 4 3 5 1 0 2 1 0-1 -2 0 2 4 0 2 4 3 5 1 0 2 St.Joseph s College of Engineering/St.Joseph s Institute of Technology 17

K=0 1 0-2 2 4 0 3 4-1 0 K=1 1 0-2 2 4 0 2 4-1 0 K=2 1 0-2 2 4 0 2 K=3 1 0-2 0 2 4 0 2 4 K=4 1 0-1 -2 0 2 4 0 2 4 3 5 1 0 2 1 0-1 -2 0 2 4 0 2 4 3 5 1 0 2 St.Joseph s College of Engineering/St.Joseph s Institute of Technology 18

1 0-1 -2 0 2 4 0 2 4 3 5 1 0 2 The cost of moving from 1 to 1, 2 to 2,... ie., self loop is 0. 1 to 2 is -1 1 to 3 is -2 1 to 4 is 0 2 to 1 is 4 2 to 3 is 2 2 to 4 is 4 3 to 1 is 5 3 to 2 is 1 3 to 4 is 2 4 to 1 is 3 4 to 2 is -1 4 to 3 is 1 St.Joseph s College of Engineering/St.Joseph s Institute of Technology 19