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

Size: px
Start display at page:

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

Transcription

1 Graph Charles Lin

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

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

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

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

6 Graph Representation

7 Graph Theory 10 B C E

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

9 epth First Search (FS) Structure to use: Stack

10 epth First Search (FS) 10 B C E

11 epth First Search (FS) 10 B C E

12 epth First Search (FS) 10 B C E

13 epth First Search (FS) 10 B C E

14 epth First Search (FS) 10 B C E

15 epth First Search (FS) 10 B B is visited already 3 C E

16 epth First Search (FS) 10 B C E

17 epth First Search (FS) 10 B C E

18 epth First Search (FS) 10 B C E

19 epth First Search (FS) 10 B C E

20 epth First Search (FS) 10 B C E We find a path from Point to Point E, but

21 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

22 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

23 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!

24 epth First Search (FS) T-Sort: 10 B C E Cycle Exists!!!

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

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

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

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

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

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

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

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

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

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

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

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

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

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

39 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

40 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

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

42 Breadth First Search (BFS) Structure to use: Queue

43 10 B Breadth First Search (BFS) Things in queue:, C 3, B C E

44 10 B Breadth First Search (BFS) Things in queue:, B 10, C, E 5, C, B 7, C, C E

45 10 B Breadth First Search (BFS) Things in queue:, C, E 5, C, B 7, C, 8, B, C 11, B, C E

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

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

48 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

49 10 ijkstra B Things in priority queue:, C 3, B Current Route: 3 C E

50 10 ijkstra B Things in priority queue:, C, E 5, C, B 7, B 10, C, Current Route:, C 3 C E

51 10 ijkstra B Things in priority queue:, C, B 7, B 10, C, Current Route:, C, E 3 C E We find the shortest path already!!!

52 * 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?

53 * 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)

54 * 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

55 * Search est = 6 Start (0 + 6 = 6) = 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

56 * Search est + 4 = = 6 Start (0 + 6 = 6) = 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

57 * Search = 6 est + 4 = = 6 Start (0 + 6 = 6) = 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

58 * Search = = = 6 est + 4 = = 6 Start (0 + 6 = 6) = 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

59 * Search = = = = 6 est + 4 = = 6 Start (0 + 6 = 6) = 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

60 * Search = = = = 6 est + 4 = = 6 Start (0 + 6 = 6) = = 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

61 * Search = = = = 6 est + 4 = = = 6 Start (0 + 6 = 6) = = = 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

62 * Search = = = = 6 est + 4 = = = 6 Start (0 + 6 = 6) = = = 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

63 * Search = = = = 6 est + 4 = = = 6 Start (0 + 6 = 6) = = = = 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

64 * Search = = = = = 6 est + 4 = = = 6 Start (0 + 6 = 6) = = = = 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

65 * Search = = = = = = 6 est + 4 = = = 6 Start (0 + 6 = 6) = = = = 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

66 * Search = = = = = = = 6 est (8 + 0 = 8) + 4 = = = 6 Start (0 + 6 = 6) = = = = 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

67 * Search = = = = = = = 6 est (8 + 0 = 8) + 4 = = = 6 Start (0 + 6 = 6) = = = = 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

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

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

70 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

71 Bellman-Ford lgorithm Time Compleity: O(VE)

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

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

74 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]); } } }

75 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

76 s t

77 s t

78 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

79 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

80 Kruskal s lgorithm 4 B C 10 E Node B C E Parent B C E

81 Kruskal s lgorithm Edge List: BC 1 4 B C 10 E Node B C E Parent B B E

82 Kruskal s lgorithm Edge List: BC 1 4 B C 10 E Node B C E Parent B B B E

83 Kruskal s lgorithm Edge List: BC 1 4 B C 10 E Node B C E Parent E

84 Cycle formed 4 B Kruskal s lgorithm Edge List: BC C 10 E Node B C E Parent E

85 Kruskal s lgorithm Edge List: BC 1 4 B C 10 E Node B C E Parent E

86 Cycle formed 4 B Kruskal s lgorithm Edge List: BC C 10 E Node B C E Parent E

87 Kruskal s lgorithm Edge List: BC 1 4 B C 10 E Node B C E Parent

88 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])); }

89 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

90 4 B Prim s lgorithm Edge List: C 3 B C 10 E

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

92 4 B Prim s lgorithm Edge List: B B 4 C 7 CE C 10 E

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

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

95 Cycle formed Prim s lgorithm 4 B Edge List: E 8 CE C 10 E

96 Prim s lgorithm Edge List: CE 10 4 B C 10 E We are done

97 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

98 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

99 Chu-Liu/Edmonds lgorithm 10 B C 8 E

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

101 Chu-Liu/Edmonds lgorithm 10 B C 8 E No cycle formed, we terminate

102 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)

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

104 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;

105 The End

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

Graph Representation DFS BFS Dijkstra A* Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp Charles Lin Graph Representation DFS BFS Dijkstra A* Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp Adjacency Matrix bool way[100][100]; cin >> i >> j; way[i][j] = true;

More information

Lecture 6 Basic Graph Algorithms

Lecture 6 Basic Graph Algorithms CS 491 CAP Intro to Competitive Algorithmic Programming Lecture 6 Basic Graph Algorithms Uttam Thakore University of Illinois at Urbana-Champaign September 30, 2015 Updates ICPC Regionals teams will be

More information

CSE 100 Minimum Spanning Trees Prim s and Kruskal

CSE 100 Minimum Spanning Trees Prim s and Kruskal CSE 100 Minimum Spanning Trees Prim s and Kruskal Your Turn The array of vertices, which include dist, prev, and done fields (initialize dist to INFINITY and done to false ): V0: dist= prev= done= adj:

More information

Introduction to Graphs. common/notes/ppt/

Introduction to Graphs.   common/notes/ppt/ Introduction to Graphs http://people.cs.clemson.edu/~pargas/courses/cs212/ common/notes/ppt/ Introduction Graphs are a generalization of trees Nodes or verticies Edges or arcs Two kinds of graphs irected

More information

Minimum Spanning Trees and Shortest Paths

Minimum Spanning Trees and Shortest Paths Minimum Spanning Trees and Shortest Paths Prim's algorithm ijkstra's algorithm November, 017 inda eeren / eoffrey Tien 1 Recall: S spanning tree Starting from vertex 16 9 1 6 10 13 4 3 17 5 11 7 16 13

More information

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

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II Anna University 2 & 16 Mark Questions & Answers Year / Semester: II / III

More information

CS6301 Programming and Data Structures II Unit -5 REPRESENTATION OF GRAPHS Graph and its representations Graph is a data structure that consists of following two components: 1. A finite set of vertices

More information

Weighted Graph Algorithms Presented by Jason Yuan

Weighted Graph Algorithms Presented by Jason Yuan Weighted Graph Algorithms Presented by Jason Yuan Slides: Zachary Friggstad Programming Club Meeting Weighted Graphs struct Edge { int u, v ; int w e i g h t ; // can be a double } ; Edge ( int uu = 0,

More information

CSE 100: GRAPH ALGORITHMS

CSE 100: GRAPH ALGORITHMS CSE 100: GRAPH ALGORITHMS Dijkstra s Algorithm: Questions Initialize the graph: Give all vertices a dist of INFINITY, set all done flags to false Start at s; give s dist = 0 and set prev field to -1 Enqueue

More information

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

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

More information

Algorithm Design (8) Graph Algorithms 1/2

Algorithm Design (8) Graph Algorithms 1/2 Graph Algorithm Design (8) Graph Algorithms / Graph:, : A finite set of vertices (or nodes) : A finite set of edges (or arcs or branches) each of which connect two vertices Takashi Chikayama School of

More information

Unweighted Graphs & Algorithms

Unweighted Graphs & Algorithms Unweighted Graphs & Algorithms Zachary Friggstad Programming Club Meeting References Chapter 4: Graph (Section 4.2) Chapter 22: Elementary Graph Algorithms Graphs Features: vertices/nodes/dots and edges/links/lines

More information

CS350: Data Structures Dijkstra s Shortest Path Alg.

CS350: Data Structures Dijkstra s Shortest Path Alg. Dijkstra s Shortest Path Alg. James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Shortest Path Algorithms Several different shortest path algorithms exist

More information

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

22 Elementary Graph Algorithms. There are two standard ways to represent a VI Graph Algorithms Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths All-Pairs Shortest Paths 22 Elementary Graph Algorithms There are two standard ways to represent a graph

More information

Outline. Graphs. Divide and Conquer.

Outline. Graphs. Divide and Conquer. GRAPHS COMP 321 McGill University These slides are mainly compiled from the following resources. - Professor Jaehyun Park slides CS 97SI - Top-coder tutorials. - Programming Challenges books. Outline Graphs.

More information

Minimum Spanning Trees and Shortest Paths

Minimum Spanning Trees and Shortest Paths Minimum Spanning Trees and Shortest Paths Kruskal's lgorithm Prim's lgorithm Shortest Paths pril 04, 018 inda eeren / eoffrey Tien 1 Kruskal's algorithm ata types for implementation Kruskalslgorithm()

More information

CAD Algorithms. Shortest Path

CAD Algorithms. Shortest Path lgorithms Shortest Path lgorithms Mohammad Tehranipoor epartment September 00 Shortest Path Problem: ind the best way of getting from s to t where s and t are vertices in a graph. est: Min (sum of the

More information

COMP 251 Winter 2017 Online quizzes with answers

COMP 251 Winter 2017 Online quizzes with answers COMP 251 Winter 2017 Online quizzes with answers Open Addressing (2) Which of the following assertions are true about open address tables? A. You cannot store more records than the total number of slots

More information

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

CS170 First Midterm Answers 17 Feb { What are the strongly connected components of the directed graph shown below? S70 First Midterm Answers 7 Feb 998 Answer to Version of Question. (3 points) { What are the strongly connected components of the directed graph shown below? A 000000 0000 00000 0 E H 0 0 00 0000 0 00

More information

Minimum Spanning Trees

Minimum Spanning Trees Minimum Spanning Trees Problem A town has a set of houses and a set of roads. A road connects 2 and only 2 houses. A road connecting houses u and v has a repair cost w(u, v). Goal: Repair enough (and no

More information

Chapter 23. Minimum Spanning Trees

Chapter 23. Minimum Spanning Trees Chapter 23. Minimum Spanning Trees We are given a connected, weighted, undirected graph G = (V,E;w), where each edge (u,v) E has a non-negative weight (often called length) w(u,v). The Minimum Spanning

More information

CS521 \ Notes for the Final Exam

CS521 \ Notes for the Final Exam CS521 \ Notes for final exam 1 Ariel Stolerman Asymptotic Notations: CS521 \ Notes for the Final Exam Notation Definition Limit Big-O ( ) Small-o ( ) Big- ( ) Small- ( ) Big- ( ) Notes: ( ) ( ) ( ) ( )

More information

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

Multiple Choice. Write your answer to the LEFT of each problem. 3 points each CSE 0-00 Test Spring 0 Name Last 4 Digits of Student ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. Suppose f ( x) is a monotonically increasing function. Which of the

More information

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

L10 Graphs. Alice E. Fischer. April Alice E. Fischer L10 Graphs... 1/37 April / 37 L10 Graphs lice. Fischer pril 2016 lice. Fischer L10 Graphs... 1/37 pril 2016 1 / 37 Outline 1 Graphs efinition Graph pplications Graph Representations 2 Graph Implementation 3 Graph lgorithms Sorting

More information

Luke. Leia 2. Han Leia

Luke. Leia 2. Han Leia Reading SE : ata Structures raph lgorithms raph Search hapter.,.,. Lecture raph T raphs are a formalism for representing relationships between objects a graph is represented as = (V, E) Han V is a set

More information

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Graphs - Introduction Kostas Alexis Terminology In the context of our course, graphs represent relations among data items G = {V,E} A graph is a set of vertices

More information

INF280 Graph Traversals & Paths

INF280 Graph Traversals & Paths INF280 Graph Traversals & Paths Florian Brandner February 27, 2018 1/21 Contents Introduction Simple Traversals Depth-First Search Breadth-First Search Finding Paths Dijkstra Bellman-Ford Floyd-Warshall

More information

Module 5 Graph Algorithms

Module 5 Graph Algorithms Module 5 Graph lgorithms Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS 97 E-mail: natarajan.meghanathan@jsums.edu 5. Graph Traversal lgorithms Depth First

More information

More Graph Algorithms: Topological Sort and Shortest Distance

More Graph Algorithms: Topological Sort and Shortest Distance More Graph Algorithms: Topological Sort and Shortest Distance Topological Sort The goal of a topological sort is given a list of items with dependencies, (ie. item 5 must be completed before item 3, etc.)

More information

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

& ( D.  mnp ' ( ) n 3. n 2. ( ) C.  n CSE Name Test Summer Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to multiply two n " n matrices is: A. " n C. "% n B. " max( m,n, p). The

More information

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

Spanning Trees. CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Motivation. Observations. Spanning tree via DFS Spanning Trees S: ata Structures & lgorithms Lecture : Minimum Spanning Trees simple problem: iven a connected undirected graph =(V,), find a minimal subset of edges such that is still connected graph

More information

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

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions Introduction Chapter 9 Graph Algorithms graph theory useful in practice represent many real-life problems can be slow if not careful with data structures 2 Definitions an undirected graph G = (V, E) is

More information

Chapter 9 Graph Algorithms

Chapter 9 Graph Algorithms Chapter 9 Graph Algorithms 2 Introduction graph theory useful in practice represent many real-life problems can be slow if not careful with data structures 3 Definitions an undirected graph G = (V, E)

More information

CS 310 Advanced Data Structures and Algorithms

CS 310 Advanced Data Structures and Algorithms CS 0 Advanced Data Structures and Algorithms Weighted Graphs July 0, 07 Tong Wang UMass Boston CS 0 July 0, 07 / Weighted Graphs Each edge has a weight (cost) Edge-weighted graphs Mostly we consider only

More information

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

4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24 /8/11 Single-Source Shortest Path Chapter 1 Shortest Paths Finding the shortest path between two nodes comes up in many applications o Transportation problems o Motion planning o Communication problems

More information

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

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list UNIT-4 Graph: Terminology, Representation, Traversals Applications - spanning trees, shortest path and Transitive closure, Topological sort. Sets: Representation - Operations on sets Applications. 1. Name

More information

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

( ). Which of ( ) ( )  #& ( )  # g( n) ( )  # f ( n) Test 1 CSE 0 Name Test Summer 006 Last Digits of Student ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to multiply two n x n matrices is: A. "( n) B. "( nlogn) # C.

More information

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

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

More information

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

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College Laura Toma Algorithms (csci2200), Bowdoin College Undirected graphs Concepts: connectivity, connected components paths (undirected) cycles Basic problems, given undirected graph G: is G connected how many

More information

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

Index. stack-based, 400 A* algorithm, 325 Index Abstract transitive closure, 174-175, 217-221 Active vertex, 411 Acyclic graph. See Digraph; Directed acyclic graph (DAG) Acyclic network, 313-321, 334-335 maxflow, 427-429 Adjacency-lists representation,

More information

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

GRAPH THEORY. What are graphs? Why graphs? Graphs are usually used to represent different elements that are somehow related to each other. GRPH THEORY Hadrian ng, Kyle See, March 2017 What are graphs? graph G is a pair G = (V, E) where V is a nonempty set of vertices and E is a set of edges e such that e = {a, b where a and b are vertices.

More information

3.1 Basic Definitions and Applications

3.1 Basic Definitions and Applications Graphs hapter hapter Graphs. Basic efinitions and Applications Graph. G = (V, ) n V = nodes. n = edges between pairs of nodes. n aptures pairwise relationship between objects: Undirected graph represents

More information

Minimum Spanning Tree

Minimum Spanning Tree Minimum Spanning Tree 1 Minimum Spanning Tree G=(V,E) is an undirected graph, where V is a set of nodes and E is a set of possible interconnections between pairs of nodes. For each edge (u,v) in E, we

More information

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

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms Professor Henry Carter Fall 2016 Recap Greedy algorithms iterate locally optimal choices to construct a globally optimal solution

More information

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

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value 1 Possible implementations Sorted linear implementations o Appropriate if

More information

Homework Assignment #3 Graph

Homework Assignment #3 Graph CISC 4080 Computer Algorithms Spring, 2019 Homework Assignment #3 Graph Some of the problems are adapted from problems in the book Introduction to Algorithms by Cormen, Leiserson and Rivest, and some are

More information

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.

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. 1. T F: Consider a directed graph G = (V, E) and a vertex s V. Suppose that for all v V, there exists a directed path in G from s to v. Suppose that a DFS is run on G, starting from s. Then, true or false:

More information

Single Source, Shortest Path Problem

Single Source, Shortest Path Problem Lecture : From ijkstra to Prim Today s Topics: ijkstra s Shortest Path lgorithm epth First Search Spanning Trees Minimum Spanning Trees Prim s lgorithm overed in hapter 9 in the textbook Some slides based

More information

Shortest Path Algorithms

Shortest Path Algorithms Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch] 1 Single Source Shortest Path Given: a directed or undirected graph G = (V,E) a source node s in V a weight function w: E

More information

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

Tree. number of vertices. Connected Graph. CSE 680 Prof. Roger Crawfis Tree Introduction to lgorithms Spanning Trees CSE Prof. Roger Crawfis We call an undirected graph a tree if the graph is connected and contains no cycles. Trees: Not Trees: Not connected Has a cycle Number

More information

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

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019 CS 341: Algorithms Douglas R. Stinson David R. Cheriton School of Computer Science University of Waterloo February 26, 2019 D.R. Stinson (SCS) CS 341 February 26, 2019 1 / 296 1 Course Information 2 Introduction

More information

CSE 100: GRAPH ALGORITHMS

CSE 100: GRAPH ALGORITHMS CSE 100: GRAPH ALGORITHMS 2 Graphs: Example A directed graph V5 V = { V = E = { E Path: 3 Graphs: Definitions A directed graph V5 V6 A graph G = (V,E) consists of a set of vertices V and a set of edges

More information

lecture29: Shortest Path Algorithms

lecture29: Shortest Path Algorithms lecture29: Shortest Path Algorithms Largely based on slides by Cinda Heeren CS 225 UIUC 30th July, 2013 Outline 1 Announcements 2 3 4 Announcements lab graphs due Thursday, 8/1 final exam this Friday (8/2),

More information

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

22 Elementary Graph Algorithms. There are two standard ways to represent a VI Graph Algorithms Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths All-Pairs Shortest Paths 22 Elementary Graph Algorithms There are two standard ways to represent a graph

More information

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Minimum Spanning Tree Kostas Alexis The Minimum Spanning Tree Algorithm A telecommunication company wants to connect all the blocks in a new neighborhood. However,

More information

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

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies: UNIT 5 CSE 103 - Unit V- Graph GRAPH Graph is another important non-linear data structure. In tree Structure, there is a hierarchical relationship between, parent and children that is one-to-many relationship.

More information

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

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs 2011 Pearson Addison-Wesley. All rights reserved 14 A-1 Terminology G = {V, E} A graph G consists of two sets A set V of vertices, or nodes A set E of edges A subgraph Consists of a subset

More information

CHAPTER 13 GRAPH ALGORITHMS

CHAPTER 13 GRAPH ALGORITHMS CHAPTER 13 GRAPH ALGORITHMS SFO LAX ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 00) AND SLIDES FROM NANCY

More information

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

Dijkstra s algorithm for shortest paths when no edges have negative weight. Lecture 14 Graph Algorithms II 14.1 Overview In this lecture we begin with one more algorithm for the shortest path problem, Dijkstra s algorithm. We then will see how the basic approach of this algorithm

More information

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

COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018 COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018 Name: ID: Answer the questions directly on the exam pages. Show all your work for each question. More detail including comments

More information

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

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018 CS 4 Section # Shortest Paths and MSTs //08 Shortest Paths There are types of shortest paths problems: Single source single destination Single source to all destinations All pairs shortest path In today

More information

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017 1 Spanning Trees, greedy algorithms Lecture 22 CS2110 Fall 2017 1 We demo A8 Your space ship is on earth, and you hear a distress signal from a distance Planet X. Your job: 1. Rescue stage: Fly your ship

More information

Lecture 10: Analysis of Algorithms (CS ) 1

Lecture 10: Analysis of Algorithms (CS ) 1 Lecture 10: Analysis of Algorithms (CS583-002) 1 Amarda Shehu November 05, 2014 1 Some material adapted from Kevin Wayne s Algorithm Class @ Princeton 1 Topological Sort Strongly Connected Components 2

More information

Shortest Paths and Minimum Spanning Trees

Shortest Paths and Minimum Spanning Trees /9/ hortest Paths and Minimum panning Trees dmin avid Kauchak cs0 pring 0 ijkstra s algorithm What about ijkstra s on? 0-0 /9/ What about ijkstra s on? ijkstra s algorithm only works for positive edge

More information

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

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013 CSE373: Data Structures & Algorithms Lecture 7: Minimum Spanning Trees Dan Grossman Fall 03 Spanning Trees A simple problem: Given a connected undirected graph G=(V,E), find a minimal subset of edges such

More information

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

CSE 332: Data Structures & Parallelism Lecture 22: Minimum Spanning Trees. Ruth Anderson Winter 2018 SE 33: Data Structures & Parallelism Lecture : Minimum Spanning Trees Ruth nderson Winter 08 Minimum Spanning Trees iven an undirected graph =(V,E), find a graph =(V, E ) such that: E is a subset of E

More information

CISC 320 Midterm Exam

CISC 320 Midterm Exam Name: CISC 320 Midterm Exam Wednesday, Mar 25, 2015 There are 19 questions. The first 15 questions count 4 points each. For the others, points are individually shown. The total is 100 points. Multiple

More information

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

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved. Chapter 9 Greedy Technique Copyright 2007 Pearson Addison-Wesley. All rights reserved. Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that

More information

Spanning trees. Suppose you have a connected undirected graph

Spanning trees. Suppose you have a connected undirected graph Spanning Trees Spanning trees Suppose you have a connected undirected graph Connected: every node is reachable from every other node Undirected: edges do not have an associated direction...then a spanning

More information

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

n 2 ( ) ( ) + n is in Θ n logn CSE Test Spring Name Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to multiply an m n matrix and a n p matrix is in: A. Θ( n) B. Θ( max(

More information

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

9. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally likely.) CSE 0 Name Test Spring 006 Last 4 Digits of Student ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. Suppose f ( x) is a monotonically increasing function. Which of the

More information

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

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u]. Tutorial Question 1 A depth-first forest classifies the edges of a graph into tree, back, forward, and cross edges. A breadth-first tree can also be used to classify the edges reachable from the source

More information

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

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions: Direct Addressing - key is index into array => O(1) lookup Hash table: -hash function maps key to index in table -if universe of keys > # table entries then hash functions collision are guaranteed => need

More information

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

10/31/18. About A6, Prelim 2. Spanning Trees, greedy algorithms. Facts about trees. Undirected trees //8 About A, Prelim Spanning Trees, greedy algorithms Lecture CS Fall 8 Prelim : Thursday, November. Visit exams page of course website and read carefully to find out when you take it (: or 7:) and what

More information

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018 1 Spanning Trees, greedy algorithms Lecture 20 CS2110 Fall 2018 1 About A6, Prelim 2 Prelim 2: Thursday, 15 November. Visit exams page of course website and read carefully to find out when you take it

More information

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

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 raphs and Trees Note Out of town Thursday afternoon Willing to meet before pm, email me if you want to meet then so I know to be in my office few extra remarks about recursion If you can write it recursively

More information

Reference Sheet for CO142.2 Discrete Mathematics II

Reference Sheet for CO142.2 Discrete Mathematics II Reference Sheet for CO14. Discrete Mathematics II Spring 017 1 Graphs Defintions 1. Graph: set of N nodes and A arcs such that each a A is associated with an unordered pair of nodes.. Simple graph: no

More information

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

Chapter 9. Greedy Algorithms: Spanning Trees and Minimum Spanning Trees msc20 Intro to lgorithms hapter. Greedy lgorithms: Spanning Trees and Minimum Spanning Trees The concept is relevant to connected undirected graphs. Problem: Here is a diagram of a prison for political

More information

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

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem Minimum Spanning Trees (Forests) Given an undirected graph G=(V,E) with each edge e having a weight w(e) : Find a subgraph T of G of minimum total weight s.t. every pair of vertices connected in G are

More information

Review of course COMP-251B winter 2010

Review of course COMP-251B winter 2010 Review of course COMP-251B winter 2010 Lecture 1. Book Section 15.2 : Chained matrix product Matrix product is associative Computing all possible ways of parenthesizing Recursive solution Worst-case running-time

More information

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

(Dijkstra s Algorithm) Consider the following positively weighted undirected graph for the problems below: 8 7 b c d h g f CS6: Algorithm Design and Analysis Recitation Section 8 Stanford University Week of 5 March, 08 Problem 8-. (Graph Representation) (a) Discuss the advantages and disadvantages of using an adjacency matrix

More information

Konigsberg Bridge Problem

Konigsberg Bridge Problem Graphs Konigsberg Bridge Problem c C d g A Kneiphof e D a B b f c A C d e g D a b f B Euler s Graph Degree of a vertex: the number of edges incident to it Euler showed that there is a walk starting at

More information

The Shortest Path Problem

The Shortest Path Problem The Shortest Path Problem 1 Shortest-Path Algorithms Find the shortest path from point A to point B Shortest in time, distance, cost, Numerous applications Map navigation Flight itineraries Circuit wiring

More information

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

Graph Algorithms. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico. Graph Algorithms Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto Superior Técnico May, 0 CPD (DEI / IST) Parallel and Distributed Computing 0-0-0 / Outline

More information

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

Undirected graph is a special case of a directed graph, with symmetric edges S-6S- ijkstra s lgorithm -: omputing iven a directed weighted graph (all weights non-negative) and two vertices x and y, find the least-cost path from x to y in. Undirected graph is a special case of a

More information

Shortest path problems

Shortest path problems Next... Shortest path problems Single-source shortest paths in weighted graphs Shortest-Path Problems Properties of Shortest Paths, Relaxation Dijkstra s Algorithm Bellman-Ford Algorithm Shortest-Paths

More information

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

Graphs. Part II: SP and MST. Laura Toma Algorithms (csci2200), Bowdoin College Laura Toma Algorithms (csci2200), Bowdoin College Topics Weighted graphs each edge (u, v) has a weight denoted w(u, v) or w uv stored in the adjacency list or adjacency matrix The weight of a path p =

More information

Graph Algorithms (part 3 of CSC 282),

Graph Algorithms (part 3 of CSC 282), Graph Algorithms (part of CSC 8), http://www.cs.rochester.edu/~stefanko/teaching/10cs8 1 Schedule Homework is due Thursday, Oct 1. The QUIZ will be on Tuesday, Oct. 6. List of algorithms covered in the

More information

CSC 344 Algorithms and Complexity

CSC 344 Algorithms and Complexity S 44 lgorithms and omplexity Lecture #1 Graphs (Extended) What is a Graph? graph consists of a set of nodes (or vertices) and a set of arcs (or edges). Each arc in a graphs is specified by a pair of nodes.

More information

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

ROOT: A node which doesn't have a parent. In the above tree. The Root is A. TREE: A tree is a finite set of one or more nodes such that there is a specially designated node called the Root, and zero or more non empty sub trees T 1, T 2...T k, each of whose roots are connected

More information

COP 4531 Complexity & Analysis of Data Structures & Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms COP 4531 Complexity & Analysis of Data Structures & Algorithms Lecture 9 Minimum Spanning Trees Thanks to the text authors who contributed to these slides Why Minimum Spanning Trees (MST)? Example 1 A

More information

Info 2950, Lecture 16

Info 2950, Lecture 16 Info 2950, Lecture 16 28 Mar 2017 Prob Set 5: due Fri night 31 Mar Breadth first search (BFS) and Depth First Search (DFS) Must have an ordering on the vertices of the graph. In most examples here, the

More information

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

Today s Outline CSE 221: Algorithms and Data Structures Graphs (with no Axes to Grind) Today s Outline S : lgorithms and ata Structures raphs (with no xes to rind) Steve Wolfman 0W Topological Sort: etting to Know raphs with a Sort raph T and raph Representations raph Terminology (a lot

More information

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

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14 CS200: Graphs Rosen Ch. 9.1-9.4, 9.6, 10.4-10.5 Walls and Mirrors Ch. 14 Trees as Graphs Tree: an undirected connected graph that has no cycles. A B C D E F G H I J K L M N O P Rooted Trees A rooted tree

More information

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

國立清華大學電機工程學系. Outline 國立清華大學電機工程學系 EE Data Structure Chapter Graph (Part I) Outline The Graph Abstract Data Type Introduction Definitions Graph Representations Elementary Graph Operations Minimum Cost Spanning Trees ch.- River

More information

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

Spanning Tree. Lecture19: Graph III. Minimum Spanning Tree (MSP) Spanning Tree (015) Lecture1: Graph III ohyung Han S, POSTH bhhan@postech.ac.kr efinition and property Subgraph that contains all vertices of the original graph and is a tree Often, a graph has many different

More information

SELF-BALANCING SEARCH TREES. Chapter 11

SELF-BALANCING SEARCH TREES. Chapter 11 SELF-BALANCING SEARCH TREES Chapter 11 Tree Balance and Rotation Section 11.1 Algorithm for Rotation BTNode root = left right = data = 10 BTNode = left right = data = 20 BTNode NULL = left right = NULL

More information

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

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science. Lecture 9 Graphs This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science. You need to be familiar with the design and use of basic data structures such as Lists, Stacks,

More information

tree follows. Game Trees

tree follows. Game Trees CPSC-320: Intermediate Algorithm Design and Analysis 113 On a graph that is simply a linear list, or a graph consisting of a root node v that is connected to all other nodes, but such that no other edges

More information

DESIGN AND ANALYSIS OF ALGORITHMS GREEDY METHOD

DESIGN AND ANALYSIS OF ALGORITHMS GREEDY METHOD 1 DESIGN AND ANALYSIS OF ALGORITHMS UNIT II Objectives GREEDY METHOD Explain and detail about greedy method Explain the concept of knapsack problem and solve the problems in knapsack Discuss the applications

More information