Graph Revisited Fei Chen CSCI2100B Data Structures Tutorial 12 1.

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.
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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph & BFS.
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.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
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.
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.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
Minimum Spanning Trees
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Graphs CS /02/05 Graphs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Definition.
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
Intro to Graphs CSIT 402 Data Structures II. CSIT 402 Graph Introduction2 Graphs Graphs are composed of ›Nodes (vertices) Can be labeled ›Edges (arcs)
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.
Chapter 2 Graph Algorithms.
GRAPH THEORY.  A graph is a collection of vertices and edges.  An edge is a connection between two vertices (or nodes).  One can draw a graph by marking.
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)
Data Structures & Algorithms Graphs
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
Discrete Structures CISC 2315 FALL 2010 Graphs & Trees.
CSC2100B Tutorial 10 Graph Jianye Hao.
CSCI2100 Data Structures Tutorial 12
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Graphs Upon completion you will be able to:
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
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.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
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.
Minimum Spanning Trees
Graph Algorithm.
Modeling and Simulation NETW 707
CSE 373 Data Structures and Algorithms
Topological Sort CSE 373 Data Structures Lecture 19.
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
GRAPHS Lecture 17 CS2110 Spring 2018.
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:

Graph Revisited Fei Chen CSCI2100B Data Structures Tutorial 12 1

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

– 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) } 3 A B C D E ABCDE ABCDE 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 4 A B CD E For example, InDegree for C is 3, OutDegree for C is 1 A B C D E ABCDE 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) } 5 A B C D E ABCDE ABCDE Graph - adjacency representation

– Adjacency list 6 A B C D E ABCDE CE D B C CD Graph - adjacency representation

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

Topological Sort 8 V1V1 V2V2 V7V7 V3V3 V6V6 V4V4 V5V5

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

10 V1V1 V2V2 V7V7 V3V3 V6V6 V4V4 V5V5 V2V2 V7V7 V3V3 V6V6 V4V4 V5V5 V7V7 V3V3 V6V6 V4V4 V5V5 V7V7 V3V3 V6V6 V4V4 V7V7 V3V3 V6V6 V7V7 V6V6 V6V6 Topological OrderV1V1 V2V2 V5V5 V4V4 V3V3 V7V7 V6V6

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. 11 Real Life Application

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

Spanning Tree 13

Minimum Spanning Tree 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

MST Algorithm 16

Kruskal’s Algorithm a bc h d gf ie EdgeWeight a bc h d gf ie a bc h d gf ie a bc h d gf ie a bc h d gf ie a bc h d gf ie

Kruskal’s Algorithm 18 EdgeWeight a bc h d gf ie a bc h d gf ie a bc h d gf ie a bc h d gf ie a bc h d gf ie a bc h d gf ie Algorithm will stop here since there are already (n-1) edges found.

19

Prim’s Algorithm a bc h d gf ie a bc h d gf ie a bc h d gf ie a bc h d gf ie a bc h d gf ie a bc h d gf ie

Prim’s Algorithm a bc h d gf ie a bc h d gf ie a bc h d gf ie a bc h d gf ie

22

MST on Non-planar Graph In graph theory, a non-planar graph cannot be drawn without edge intersections. 23

Prim’s Algorithm 24 a cb de a cb de a cb de a cb de a cb de a cb de

Kruskal’s Algorithm 25 a cb de How to apply Kruskal’s Algorithm in this non- planar graph to find the minimum spanning tree? Solve it yourself.

Shortest Path Problem 26

Real Life Application Travelling Salesman Problem It’s the problem of finding the shortest path that goes through every node exactly once, and returns to the start. 27

Dijkstra’s Algorithm 28 Dest The shortest path from V 0 to other vertices V1V1 Step 1 V2V2 V3V3 V4V4 VjVj Step 2Step 3Step 4Step 5 S V5V5 {V 0, V 2, V 3, V 4, V 5 } Inf 10 (V 0, V 2 ) Inf 60 (V 0, V 2, V 3 ) 50 (V 0, V 4, V 3 ) 30 (V 0, V 4 ) 30 (V 0, V 4 ) 100 (V 0, V 5 ) 100 (V 0, V 5 ) 90 (V 0, V 4, V 5 ) 60 (V 0, V 4, V 3, V 5 ) V2V4V3V5 {V 0, V 2, V 3, V 4 }{V 0, V 2, V 4 }{V 0, V 2 } V0V0 V5V5 V1V1 V2V2 V3V3 V4V

29