CSCI2100 Data Structures Tutorial

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Chapter 9: Graphs Topological Sort
Graph Theory Arnold Mesa. Basic Concepts n A graph G = (V,E) is defined by a set of vertices and edges v3 v1 v2Vertex (v1) Edge (e1) A Graph with 3 vertices.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
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.
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.
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.
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.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
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
Chapter 2 Graph Algorithms.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
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.
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.
CSC2100B Tutorial 10 Graph Jianye Hao.
CSCI2100 Data Structures Tutorial 12
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:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
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 Representation, BFS, DFS
Fundamentals, Terminology, Traversal, Algorithms
Minimum Spanning Tree Chapter 13.6.
C.Eng 213 Data Structures Graphs Fall Section 3.
CISC 235: Topic 10 Graph Algorithms.
Topological Sort (topological order)
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
ITEC 2620M Introduction to Data Structures
CSE 373: Data Structures and Algorithms
Text Book: Introduction to algorithms By C L R S
Autumn 2016 Lecture 10 Minimum Spanning Trees
CSC 380: Design and Analysis of Algorithms
Chapter 16 1 – Graphs Graph Categories Strong Components
Data Structures and Algorithm Analysis Graph Algorithms
CSE 417: Algorithms and Computational Complexity
Graph Algorithm.
Graphs G = (V,E) V is the vertex set.
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 Graph Revisited Hongyi Zhang CSCI2100 Data Structures Tutorial 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 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 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 - Complexity

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. 8 8

Topological Sort V1 V2 V3 V4 V5 V6 V7 9 9

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. 10 10

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

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. 12 12

Spanning Tree 13 13

Minimum Spanning Tree 14 14

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. 15 15

MST Algorithm 16 16

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 17 17

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. 18 18

19 19

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 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 21 21

22 22

Shortest Path Problem 23 23

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} 24 24

25 25