CSCI2100 Data Structures Tutorial 12

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Chapter 9: Graphs Topological Sort
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Minimum Spanning Tree CSE 331 Section 2 James Daly.
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
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.
Connected Components, Directed Graphs, Topological Sort COMP171.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Connected Components, Directed Graphs, Topological Sort Lecture 25 COMP171 Fall 2006.
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. Edges are.
Connected Components, Directed graphs, Topological sort COMP171 Fall 2005.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
WEIGHTED GRAPHS. Weighted Graphs zGraph G = (V,E) such that there are weights/costs associated with each edge Õw((a,b)): cost of edge (a,b) Õrepresentation:
CS 146: Data Structures and Algorithms July 21 Class Meeting
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Charles Lin. Graph Representation Graph Representation DFS DFS BFS BFS Dijkstra Dijkstra A* Search A* Search Bellman-Ford Bellman-Ford Floyd-Warshall.
CS 3343: Analysis of Algorithms Lecture 21: Introduction to Graphs.
Chapter 2 Graph Algorithms.
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.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
CSC2100B Tutorial 10 Graph Jianye Hao.
Graphs Upon completion you will be able to:
Graph Revisited Fei Chen CSCI2100B Data Structures Tutorial 12 1.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Shortest Path -Prim’s -Djikstra’s. PRIM’s - Minimum Spanning Tree -A spanning tree of a graph is a tree that has all the vertices of the graph connected.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
Minimum Spanning Trees
Graphs Lecture 19 CS2110 – Spring 2013.
Graphs Representation, BFS, DFS
Minimum Spanning Tree Chapter 13.6.
CISC 235: Topic 10 Graph Algorithms.
CS120 Graphs.
Graph Algorithm.
Minimum-Cost Spanning Tree
CS 3343: Analysis of Algorithms
Minimum Spanning Tree.
Minimum Spanning Trees
Connected Components Minimum Spanning Tree
Graph Algorithm.
Modeling and Simulation NETW 707
CSE 373 Data Structures and Algorithms
Minimum-Cost Spanning Tree
4-4 Graph Theory Trees.
Chapter 23 Minimum Spanning Tree
Minimum-Cost Spanning Tree
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
Chapter 11 Graphs.
Connected Components, Directed Graphs, Topological Sort
CSCI2100 Data Structures Tutorial
CSE 373: Data Structures and Algorithms
Text Book: Introduction to algorithms By C L R S
Data Structures and Algorithm Analysis Graph Algorithms
CSE 373: Data Structures and Algorithms
Graph Algorithm.
Graphs G = (V,E) V is the vertex set.
GRAPHS.
Minimum-Cost Spanning Tree
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

CSCI2100 Data Structures Tutorial 12 Graph Revisited Yang Zhengwei CSCI2100 Data Structures Tutorial 12 1 1

Outline Graph Adjacency Representation Topological Sort Minimum Spanning Tree Kruskal’s Algorithm Prim’s Algorithm Shortest Path Dijkstra’ Algorithm 2 2

Graph - adjacency representation Adjacency matrix G = (V, E) V = { A, B, C, D, E } E = { (A, C), (A, E), (B, D), (C, B), (D, C), (E, C), (E, D) } B A B C D E A 1 1 C D B 1 C 1 D 1 E 1 1 A E 3

Graph - adjacency representation Degree of a vertex v number of edges incident on that vertex For directed graph, Degree = InDegree + OutDegree InDegree of a vertex v sum of column v OutDegree of a vertex v sum of row v A B C D E A B C D E A 1 1 B 1 C 1 D 1 E 1 1 For example, InDegree for C is 3, OutDegree for C is 1 4

Graph - adjacency representation B C D E 1 5

Graph - adjacency representation Adjacency list B A C E B D C D C B D C E C D A E 6

Graph - adjacency representation Adjacency list 7

Graph - Complexity 8

Topological Sort A topological sort of a DAG (Directed Acyclic Graph) G is a linear ordering of all its vertices such that if G contains an edge (u, v), then u appears before v in the ordering. u v 9 9

Topological Sort Topological Order: V1 V2 V5 V4 V3 V7 V6 V1 V2 V3 V4 10 10

How to find the topological ordering? Define the indegree of a vertex v as the # of edges (u,v). We compute the indegrees of all vertices in the graph. Find any vertex with no incoming edges (or the indegree is 0). We print this vertex, and remove it, along with its edges, from the graph. Then we apply this same strategy to the rest of the graph. 11 11

Topological Order V1 V2 V5 V4 V3 V7 V6 V1 V2 V2 V3 V4 V5 V3 V4 V5 V6 12 12

Real Life Application Course prerequisite in university studies A directed edge (u,v) indicates that course u must be completed before course v may be attempted A topological ordering of these courses is any course sequence that does not violate the prerequisite requirement. 14 14

Spanning Tree 15 15

Minimum Spanning Tree 16 16

Real Life Application One example would be a cable TV company laying cable to a new neighborhood. The graph represents which houses are connected by those cables. A spanning tree for this graph would be a subset of those paths that has no cycles but still connects to every house. There might be several spanning trees possible. A minimum spanning tree would be one with the lowest total cost. 17 17

MST Algorithm 18 18

Kruskal’s Algorithm a b c h d g f i e a b c h d g f i e a b c h d g f 7 8 a b c h d g f i e 4 14 9 1 10 11 2 6 a b c h d g f i e 4 8 7 14 9 1 10 11 2 6 Edge Weight <h, g> 1 <c, i> 2 <g, f> <a, b> 4 <c, f> <g, i> 6 <c, d> 7 <h, i> <a, h> 8 <b, c> <d, e> 9 <e, f> 10 <b, h> 11 <d, f> 14 a b c h d g f i e 4 8 7 14 9 1 10 11 2 6 a b c h d g f i e 4 8 7 14 9 1 10 11 2 6 a b c h d g f i e 4 8 7 14 9 1 10 11 2 6 a b c h d g f i e 4 8 14 9 1 10 11 2 7 6 19 19

Algorithm will stop here since there are already (n-1) edges found. Kruskal’s Algorithm a b c h d g f i e 4 8 14 9 1 10 11 2 7 6 a b c h d g f i e 4 8 14 9 1 10 11 2 7 6 Edge Weight <h, g> 1 <c, i> 2 <g, f> <a, b> 4 <c, f> <g, i> 6 <c, d> 7 <h, i> <a, h> 8 <b, c> <d, e> 9 <e, f> 10 <b, h> 11 <d, f> 14 a b c h d g f i e 4 8 14 9 1 10 11 2 7 6 a b c h d g f i e 4 8 14 9 1 10 11 2 7 6 a b c h d g f i e 4 8 14 9 1 10 11 2 7 6 a b c h d g f i e 4 8 14 9 1 10 11 2 7 6 Algorithm will stop here since there are already (n-1) edges found. 20 20

21 21

Prim’s Algorithm a b c h d g f i e a b c h d g f i e a b c h d g f i e 7 8 a b c h d g f i e 4 14 9 1 10 11 2 6 7 8 a b c h d g f i e 4 14 9 1 10 11 2 6 7 8 a b c h d g f i e 4 14 9 1 10 11 2 6 7 8 a b c h d g f i e 4 14 9 1 10 11 2 6 7 8 a b c h d g f i e 4 14 9 1 10 11 2 6 7 8 a b c h d g f i e 4 14 9 1 10 11 2 6 22 22

Prim’s Algorithm a b c h d g f i e a b c h d g f i e a b c h d g f i e 7 8 a b c h d g f i e 4 14 9 1 10 11 2 6 7 8 a b c h d g f i e 4 14 9 1 10 11 2 6 7 8 a b c h d g f i e 4 14 9 1 10 11 2 6 7 8 a b c h d g f i e 4 14 9 1 10 11 2 6 23 23

24 24

Shortest Path Problem 25 25

The shortest path from V0 to other vertices Dijkstra’s Algorithm V5 60 100 V0 V4 30 10 20 10 V1 V3 50 5 V2 Dest The shortest path from V0 to other vertices Step 1 Step 2 Step 3 Step 4 Step 5 V1 Inf Inf Inf Inf Inf V2 10 (V0, V2) V3 Inf 60 (V0, V2, V3) 50 (V0, V4, V3) V4 30 (V0, V4) 30 (V0, V4) V5 100 (V0, V5) 100 (V0, V5) 90 (V0, V4, V5) 60 (V0, V4, V3, V5) Vj V2 V4 V3 V5 S {V0, V2} {V0, V2, V4} {V0, V2, V3, V4} {V0, V2, V3, V4, V5} 26 26

27 27