Agenda. Graph Representation DFS BFS Dijkstra A* Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp

Similar documents
Graph Representation DFS BFS Dijkstra A* Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp

Lecture 6 Basic Graph Algorithms

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Introduction to Graphs. common/notes/ppt/

Minimum Spanning Trees and Shortest Paths

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II


Weighted Graph Algorithms Presented by Jason Yuan

CSE 100: GRAPH ALGORITHMS

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Algorithm Design (8) Graph Algorithms 1/2

Unweighted Graphs & Algorithms

CS350: Data Structures Dijkstra s Shortest Path Alg.

22 Elementary Graph Algorithms. There are two standard ways to represent a

Outline. Graphs. Divide and Conquer.

Minimum Spanning Trees and Shortest Paths

CAD Algorithms. Shortest Path

COMP 251 Winter 2017 Online quizzes with answers

CS170 First Midterm Answers 17 Feb { What are the strongly connected components of the directed graph shown below?

Minimum Spanning Trees

Chapter 23. Minimum Spanning Trees

CS521 \ Notes for the Final Exam

Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

L10 Graphs. Alice E. Fischer. April Alice E. Fischer L10 Graphs... 1/37 April / 37

Luke. Leia 2. Han Leia

CS302 - Data Structures using C++

INF280 Graph Traversals & Paths

Module 5 Graph Algorithms

More Graph Algorithms: Topological Sort and Shortest Distance

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

Spanning Trees. CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Motivation. Observations. Spanning tree via DFS

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Chapter 9 Graph Algorithms

CS 310 Advanced Data Structures and Algorithms

4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

( ). Which of ( ) ( ) " #& ( ) " # g( n) ( ) " # f ( n) Test 1

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Index. stack-based, 400 A* algorithm, 325

GRAPH THEORY. What are graphs? Why graphs? Graphs are usually used to represent different elements that are somehow related to each other.

3.1 Basic Definitions and Applications

Minimum Spanning Tree

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Homework Assignment #3 Graph

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

Single Source, Shortest Path Problem

Shortest Path Algorithms

Tree. number of vertices. Connected Graph. CSE 680 Prof. Roger Crawfis

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

CSE 100: GRAPH ALGORITHMS

lecture29: Shortest Path Algorithms

22 Elementary Graph Algorithms. There are two standard ways to represent a

CS302 - Data Structures using C++

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

CHAPTER 13 GRAPH ALGORITHMS

Dijkstra s algorithm for shortest paths when no edges have negative weight.

COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

Lecture 10: Analysis of Algorithms (CS ) 1

Shortest Paths and Minimum Spanning Trees

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

CSE 332: Data Structures & Parallelism Lecture 22: Minimum Spanning Trees. Ruth Anderson Winter 2018

CISC 320 Midterm Exam

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Spanning trees. Suppose you have a connected undirected graph

n 2 ( ) ( ) + n is in Θ n logn

9. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally likely.)

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

10/31/18. About A6, Prelim 2. Spanning Trees, greedy algorithms. Facts about trees. Undirected trees

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018

Note. Out of town Thursday afternoon. Willing to meet before 1pm, me if you want to meet then so I know to be in my office

Reference Sheet for CO142.2 Discrete Mathematics II

Chapter 9. Greedy Algorithms: Spanning Trees and Minimum Spanning Trees

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

Review of course COMP-251B winter 2010

(Dijkstra s Algorithm) Consider the following positively weighted undirected graph for the problems below: 8 7 b c d h g f

Konigsberg Bridge Problem

The Shortest Path Problem

Graph Algorithms. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

Undirected graph is a special case of a directed graph, with symmetric edges

Shortest path problems

Graphs. Part II: SP and MST. Laura Toma Algorithms (csci2200), Bowdoin College

Graph Algorithms (part 3 of CSC 282),

CSC 344 Algorithms and Complexity

ROOT: A node which doesn't have a parent. In the above tree. The Root is A.

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Info 2950, Lecture 16

Today s Outline CSE 221: Algorithms and Data Structures Graphs (with no Axes to Grind)

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

國立清華大學電機工程學系. Outline

Spanning Tree. Lecture19: Graph III. Minimum Spanning Tree (MSP)

SELF-BALANCING SEARCH TREES. Chapter 11

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

tree follows. Game Trees

DESIGN AND ANALYSIS OF ALGORITHMS GREEDY METHOD

Transcription:

Graph Charles Lin

genda Graph Representation FS BFS ijkstra * Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp

Graph Representation djacency Matrix bool way[100][100]; cin >> i >> j; way[i][j] = true;

Graph Representation djacency Linked List vector<int> v[100]; cin >> i >> j; v[i].push_back(j);

Graph Representation Edge List struct Edge { int Start_Vertex, End_Vertex; } edge[10000]; cin >> edge[i].start_vertex >> edge[i].end_vertex;

Graph Representation

Graph Theory 10 B 1 4 8 7 9 3 C E

Graph Theory Mission: To go from Point to Point E How?

epth First Search (FS) Structure to use: Stack

epth First Search (FS) 10 B 1 4 8 7 9 3 C E

epth First Search (FS) 10 B 1 4 8 7 9 3 C E

epth First Search (FS) 10 B 1 4 8 7 9 3 C E

epth First Search (FS) 10 B 1 4 8 7 9 3 C E

epth First Search (FS) 10 B 1 4 8 7 9 3 C E

epth First Search (FS) 10 B 1 4 8 7 9 B is visited already 3 C E

epth First Search (FS) 10 B 1 4 8 7 9 3 C E

epth First Search (FS) 10 B 1 4 8 7 9 3 C E

epth First Search (FS) 10 B 1 4 8 7 9 3 C E

epth First Search (FS) 10 B 1 4 8 7 9 3 C E

epth First Search (FS) 10 B 1 4 8 7 9 3 C E We find a path from Point to Point E, but

epth First Search (FS) stack<int> s; bool Visited[MX]; void FS(int u) { } s.push(u); if (u == GOL) { } for (int x = 0; x < s.size(); x++) return ; printf( %d, s[x]); for (int x = 0; x < MX; x++) if (!Visited[x]) { } s.pop(); Visited[x] = true; FS(x); Visited[x] = false; int main() { memset(visited, 0, sizeof(visited)); // Input Handling (GOL =?) FS(0); } Very Important It needs to be restored for other points to traverse, or the path may not be found

epth First Search (FS) Usage: Finding a path from start to destination (NOT RECOMMENE TLE) Topological Sort (T-Sort) Strongly-Connected Components (SCC) etect cycles

epth First Search (FS) Topological Sort irected cyclic Graph (G) Find the order of nodes such that for each node, every parent is before that node on the list umb method: Check for root of residual graph (o it n times) Better Method: Reverse of finishing time Start from all vertices!

epth First Search (FS) T-Sort: 10 B 1 4 8 7 9 3 C E Cycle Exists!!!

epth First Search (FS) T-Sort: B C E OK

epth First Search (FS) T-Sort: B C E Things in output stack: NONE

epth First Search (FS) T-Sort: B C E Things in output stack: NONE

epth First Search (FS) T-Sort: B C E Things in output stack: NONE

epth First Search (FS) T-Sort: B C E Things in output stack: NONE

epth First Search (FS) T-Sort: B C E Things in output stack: E

epth First Search (FS) T-Sort: B C E Things in output stack: E,

epth First Search (FS) T-Sort: B C E Things in output stack: E,, B

epth First Search (FS) T-Sort: B C E Things in output stack: E,, B

epth First Search (FS) T-Sort: B C E Things in output stack: E,, B

epth First Search (FS) T-Sort: B C E Things in output stack: E,, B

epth First Search (FS) T-Sort: B C E Things in output stack: E,, B

epth First Search (FS) T-Sort: B C E Things in output stack: E,, B, C

epth First Search (FS) T-Sort: B C E Things in output stack: E,, B, C, T-Sort Output:, C, B,, E

epth First Search (FS) Strongly Connected Components irected Graph Find groups of nodes such that in each group, every node has a path to every other node

epth First Search (FS) Strongly Connected Components Method: FS twice (Kosaraju) o FS on graph G from all vertices, record finishing time of node Reverse direction of edges o FS on Graph G from all vertices sorted by decreasing finishing time Each FS tree is an SCC

epth First Search (FS) Strongly Connected Components Note: fter grouping the vertices, the new nodes form a irected cyclic Graph

Breadth First Search (BFS) Structure to use: Queue

10 B Breadth First Search (BFS) Things in queue:, C 3, B 10 1 4 8 7 9 3 C E

10 B Breadth First Search (BFS) Things in queue:, B 10, C, E 5, C, B 7, C, 8 1 4 8 7 9 3 C E

10 B Breadth First Search (BFS) Things in queue:, C, E 5, C, B 7, C, 8, B, C 11, B, 1 1 4 8 7 9 3 C E

10 B Breadth First Search (BFS) Things in queue:, C, B 7, C, 8, B, C 11, B, 1 1 4 8 7 9 3 C E We are done!!!

Breadth First Search (BFS) pplication: Finding shortest path Flood-Fill

ijkstra Structure to use: Priority Queue Consider the past The length of path visited so far No negative edges allowed Maintain known vertices t each step: Take the unknown vertex with smallest overall estimate

10 ijkstra B Things in priority queue:, C 3, B 10 1 4 8 7 9 Current Route: 3 C E

10 ijkstra B Things in priority queue:, C, E 5, C, B 7, B 10, C, 11 1 4 8 7 9 Current Route:, C 3 C E

10 ijkstra B Things in priority queue:, C, B 7, B 10, C, 11 1 4 8 7 9 Current Route:, C, E 3 C E We find the shortest path already!!!

* Search Structure to use: Priority Queue Consider the past + future How to consider the future? dmissible Heuristic (Best-Case Prediction: must never overestimate) How to predict?

* Search Prediction 1: isplacement Given coordinates, calculate the displacement of current node to destination sqrt((x x1) + (y y1) ) Prediction : Manhattan istance Given grid, calculate the horizontal and vertical movement (x x1) + (y y1)

* Search est Start - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order

* Search est 1 + 5 = 6 Start (0 + 6 = 6) 1 + 5 = 6 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order

* Search est + 4 = 6 1 + 5 = 6 Start (0 + 6 = 6) 1 + 5 = 6 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order

* Search 3 + 3 = 6 est + 4 = 6 1 + 5 = 6 Start (0 + 6 = 6) 1 + 5 = 6 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order

* Search 4 + 4 = 8 3 + 3 = 6 4 + = 6 est + 4 = 6 1 + 5 = 6 Start (0 + 6 = 6) 1 + 5 = 6 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order

* Search 4 + 4 = 8 5 + 3 = 8 3 + 3 = 6 4 + = 6 est + 4 = 6 1 + 5 = 6 Start (0 + 6 = 6) 1 + 5 = 6 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order

* Search 4 + 4 = 8 5 + 3 = 8 3 + 3 = 6 4 + = 6 est + 4 = 6 1 + 5 = 6 Start (0 + 6 = 6) 1 + 5 = 6 + 4 = 6 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order

* Search 4 + 4 = 8 5 + 3 = 8 3 + 3 = 6 4 + = 6 est + 4 = 6 1 + 5 = 6 3 + 3 = 6 Start (0 + 6 = 6) 1 + 5 = 6 + 4 = 6 3 + 3 = 6 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order

* Search 4 + 4 = 8 5 + 3 = 8 3 + 3 = 6 4 + = 6 est + 4 = 6 1 + 5 = 6 3 + 3 = 6 Start (0 + 6 = 6) 1 + 5 = 6 + 4 = 6 3 + 3 = 6 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order

* Search 4 + 4 = 8 5 + 3 = 8 3 + 3 = 6 4 + = 6 est + 4 = 6 1 + 5 = 6 3 + 3 = 6 Start (0 + 6 = 6) 1 + 5 = 6 + 4 = 6 3 + 3 = 6 4 + 4 = 8 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order

* Search 4 + 4 = 8 5 + 3 = 8 6 + = 8 3 + 3 = 6 4 + = 6 est + 4 = 6 1 + 5 = 6 3 + 3 = 6 Start (0 + 6 = 6) 1 + 5 = 6 + 4 = 6 3 + 3 = 6 4 + 4 = 8 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order

* Search 4 + 4 = 8 5 + 3 = 8 6 + = 8 7 + 1 = 8 3 + 3 = 6 4 + = 6 est + 4 = 6 1 + 5 = 6 3 + 3 = 6 Start (0 + 6 = 6) 1 + 5 = 6 + 4 = 6 3 + 3 = 6 4 + 4 = 8 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order

* Search 4 + 4 = 8 5 + 3 = 8 6 + = 8 7 + 1 = 8 8 + = 10 3 + 3 = 6 4 + = 6 est (8 + 0 = 8) + 4 = 6 1 + 5 = 6 3 + 3 = 6 Start (0 + 6 = 6) 1 + 5 = 6 + 4 = 6 3 + 3 = 6 4 + 4 = 8 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order

* Search 4 + 4 = 8 5 + 3 = 8 6 + = 8 7 + 1 = 8 8 + = 10 3 + 3 = 6 4 + = 6 est (8 + 0 = 8) + 4 = 6 1 + 5 = 6 3 + 3 = 6 Start (0 + 6 = 6) 1 + 5 = 6 + 4 = 6 3 + 3 = 6 4 + 4 = 8 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order

* Search * Search = Past + Future * Search Future =? ijkstra * Search Past =? Greedy

Graph Theory How about graph with negative edges? Bellman-Ford

Bellman-Ford lgorithm Consider the source as weight 0, others as infinity Count = 0, Improve = true While (Count < n and Improve) { Improve = false Count++ For each edge uv If u.weight + uv distance < v.weight { } Improve = true v.weight = u.weight + uv.distance } If (Count == n) print Negative weight cycle detected Else print shortest path distance

Bellman-Ford lgorithm Time Compleity: O(VE)

Graph Theory ll we have just dealt with is single source How about multiple sources (all sources)? Floyd-Warshall lgorithm

Floyd-Warshall lgorithm 1. Compute on subgraph {1}. Compute on subgraph {1,} 3. Compute on subgraph {1,..., 3} 4.... N. Compute on entire graph one!

Floyd-Warshall lgorithm For (int k = 0; k < MX; k++) { For (int i = 0; i < MX; i++) { For (int j = 0; j < MX; j++) { Path[i][j] = min(path[i][j], Path[i][k] + Path[k][j]); } } }

Iterative? Non-iterative? What is iterative-deepening? Given limited time, f nd the current most optimal solution ijkstra (Shortest 1-step Solution) ijkstra (Shortest -steps Solution) ijkstra (Shortest 3-steps Solution) ijkstra (Shortest n-steps Solution) Suitable in bi-directional search (Faster than 1-way search): only for FS, tricky otherwise

s t

s t

Minimum Spanning Tree (MST) Given an undirected graph, find the minimum cost so that every node has path to other nodes Kruskal s lgorithm Prim s lgorithm

Kruskal s lgorithm Continue Finding Shortest Edge If no cycle is formed add it into the list Construct a parent-child relationship If all nodes have the same root, we are done (path compression) General idea: union-find, may be useful in other situations

Kruskal s lgorithm 4 B 1 7 8 3 C 10 E Node B C E Parent B C E

Kruskal s lgorithm Edge List: BC 1 4 B 1 7 8 3 C 10 E Node B C E Parent B B E

Kruskal s lgorithm Edge List: BC 1 4 B 1 7 8 3 C 10 E Node B C E Parent B B B E

Kruskal s lgorithm Edge List: BC 1 4 B 1 7 8 3 C 10 E Node B C E Parent E

Cycle formed 4 B Kruskal s lgorithm Edge List: BC 1 1 7 8 3 C 10 E Node B C E Parent E

Kruskal s lgorithm Edge List: BC 1 4 B 1 7 8 3 C 10 E Node B C E Parent E

Cycle formed 4 B Kruskal s lgorithm Edge List: BC 1 1 7 8 3 C 10 E Node B C E Parent E

Kruskal s lgorithm Edge List: BC 1 4 B 1 7 8 3 C 10 E Node B C E Parent

Kruskal s lgorithm Path Compression can be done when we find root int find_root(int x) { return (parent[x] == x)? x: return (parent[x] = find_root(parent[x])); }

Prim s lgorithm While not all nodes are visited While the list is not empty and the minimum edge connects to visited node Pop out the edge If the list is empty, print Impossible and return Else Consider popped edge Mark its endpoint as visited dd all its unvisited edges to the list Print the tree

4 B Prim s lgorithm Edge List: C 3 B 4 1 7 8 3 C 10 E

4 B Prim s lgorithm Edge List: CB 1 B 4 C 7 CE 10 1 7 8 3 C 10 E

4 B Prim s lgorithm Edge List: B B 4 C 7 CE 10 1 7 8 3 C 10 E

4 B Prim s lgorithm Edge List: B 4 C 7 E 8 CE 10 1 7 8 3 C 10 E

Cycle formed Prim s lgorithm 4 B Edge List: C 7 E 8 CE 10 1 7 8 3 C 10 E

Cycle formed Prim s lgorithm 4 B Edge List: E 8 CE 10 1 7 8 3 C 10 E

Prim s lgorithm Edge List: CE 10 4 B 1 7 8 3 C 10 E We are done

irected Minimum Spanning Tree We have just dealt with undirected MST. How about directed? Using Kruskal s or Prim s cannot solve directed MST problem How? Chu-Liu/Edmonds lgorithm

Chu-Liu/Edmonds lgorithm For each vertex, find the smallest incoming edge While there is cycle Find an edge entering the cycle with smallest increase Remove the edge pointing to the same node and replace by new edge Print out the tree

Chu-Liu/Edmonds lgorithm 10 B 4 5 7 9 3 C 8 E

Chu-Liu/Edmonds lgorithm 10 B 4 5 7 9 3 C 8 E Cycle formed (B, C, ) => B, C, = 10 = 8 => B, C, = 3 = 1 E => B, C, = 9 4 = 5

Chu-Liu/Edmonds lgorithm 10 B 4 5 7 9 3 C 8 E No cycle formed, we terminate

Chu-Liu/Edmonds lgorithm Using this algorithm may unloop a cycle and create another cycle But since the length of tree is increasing, there will have an end eventually Worst Case is doing n-1 times Time Complexity is O(EV)

Flow graph with weight (capacity) Mission: Find out the maximum flow from source to destination How?

Edmond-Karp o { o BFS on the graph to find maximum flow in the graph dd it to the total flow For each edge in the maximum flow educt the flow just passed } While the flow is not zero;

The End