CSC2100B Tutorial 10 Graph Jianye Hao.

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.
1 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
Minimum Spanning Tree CSE 331 Section 2 James Daly.
Minimum cost spanning tree and activity networks Data structure 2002/12/2.
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
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.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
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.
Minimum Spanning Trees CIS 606 Spring Problem A town has a set of houses and a set of roads. A road connects 2 and only 2 houses. A road connecting.
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.
CS2420: Lecture 36 Vladimir Kulyukin Computer Science Department Utah State University.
Minimum Spanning Trees
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.
CS 146: Data Structures and Algorithms July 21 Class Meeting
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
CS 3343: Analysis of Algorithms Lecture 21: Introduction to Graphs.
Chapter 2 Graph Algorithms.
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
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.
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
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.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CSCI2100 Data Structures Tutorial 12
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
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:
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
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.
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.
Greedy Technique.
Minimum Spanning Tree Chapter 13.6.
C.Eng 213 Data Structures Graphs Fall Section 3.
CISC 235: Topic 10 Graph Algorithms.
CS120 Graphs.
Graph Algorithm.
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
4-4 Graph Theory Trees.
Topological Sort CSE 373 Data Structures Lecture 19.
Chapter 23 Minimum Spanning Tree
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
Chapter 11 Graphs.
CSCI2100 Data Structures Tutorial
CSE 373: Data Structures and Algorithms
Data Structures and Algorithm Analysis Graph Algorithms
CSE 373: Data Structures and Algorithms
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:

CSC2100B Tutorial 10 Graph Jianye Hao

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

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

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

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 1 C 1 1 1 1 D 1 1 1 E 1 1 1 A E

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

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.

Topological Sort V1 V2 V3 V4 V5 V6 V7

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.

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

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

Notes Is topological ordering possible with a cyclic graph? No, since for two vertices v and w on the cycle, v precedes w and w precedes v. Is the ordering unique? It is not necessarily unique; any legal ordering will do (e.g., v1, v2, v5, v4, v3, v7, v6 and v1, v2, v5, v4, v7, v3, v6 are topological orderings).

Spanning Tree

Minimum Spanning Tree

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.

MST Algorithm

17 17

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

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

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

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

The End Any Questions?