Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Data Structures and Algorithms Graphs Single-Source Shortest Paths (Dijkstra’s Algorithm) PLSD210(ii)
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Chapter 8, Part I Graph Algorithms.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
Graph.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
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.
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.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
DAST 2005 Tirgul 12 (and more) sample questions. DAST 2005 Q.We’ve seen that solving the shortest paths problem requires O(VE) time using the Belman-Ford.
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.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
1.1 Data Structure and Algorithm Lecture 13 Minimum Spanning Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Minimum Spanning Trees.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
CS200 Algorithms and Data StructuresColorado State University Part 10. Graphs CS 200 Algorithms and Data Structures 1.
COSC 2007 Data Structures II Chapter 14 Graphs III.
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
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 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.
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 Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
Homework #5 Due: October 31, 2000 Christine Kang Graph Concepts and Algorithms.
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:
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Chapter 05 Introduction to Graph And Search Algorithms.
GRAPH THEORY Discrete Math Team KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS )
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
Minimum Spanning Trees
Graphs Representation, BFS, DFS
Introduction to Graphs
UCS 406 – Data Structures & Algorithms
CS120 Graphs.
Comp 245 Data Structures Graphs.
Graphs Representation, BFS, DFS
Minimum Spanning Trees
Connected Components Minimum Spanning Tree
Graphs Chapter 13.
Minimum Spanning Trees
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
Chapter 11 Graphs.
Minimum Spanning Tree Algorithms
CSCI2100 Data Structures Tutorial
Algorithms Searching in a Graph.
Minimum spanning tree Shortest path algorithms
Weighted Graphs & Shortest Paths
CE 221 Data Structures and Algorithms
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Dijkstra's Shortest Path Algorithm
CE 221 Data Structures and Algorithms
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Graphs: Shortest path and mst
Single-Source Shortest Path & Minimum Spanning Trees
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 .
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Graph II MST, Shortest Path

Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple path Cycle Directed acyclic graph Weighted graph

Graph representation Adjacency Matrix Assume N nodes in graph Use Matrix A[0…N-1][0…N-1]  if vertex i and vertex j are adjacent in graph, A[i][j] = 1,  otherwise A[i][j] = 0  if vertex i has a loop, A[i][i] = 1  if vertex i has no loop, A[i][i] = 0

Graph representation Adjacency List An array of list the ith element of the array is a list of vertices that connect to vertex i vertex 0 connect to vertex 1, 2 and 3 vertex 1 connects to 3 vertex 2 connects to 3

Graph Traversal From one vertex, list out all vertices that can be reached in graph G Set of nodes to expand Each node has a flag to indicate visited or not Depth First Traversal Breadth First Traversal

Spanning Tree Connected subgraph that includes all vertices of the original connected graph Subgraph is a tree  If original graph has n vertices, the spanning tree has n vertices and n-1 edges.  No circle in this subgraph

Spanning Tree Minimum number of edges to keep it connected If N vertices, spanning tree has N-1 edges

Minimum Spanning Tree (MST) Spanning tree with minimum weight

Prim’s Algorithm For Finding MST 1. All nodes are unselected, mark node v selected 2. For each node of graph, { Find the edge with minimum weight that connects an unselected node with a selected node Mark this unselected node as selected }

Demos For Finding MST Step 1: mark vertex A as selected A B C D

Demos For Finding MST Step 2: find the minimum weighted edge connected to vertex A, and mark the other vertex on this edge as selected. A B C D

Demos For Finding MST Step 3: find the minimum weighted edge connected to vertices set { A, D }, and mark the other vertex on this edge as selected. A B C D

Demos For Finding MST Step 4: find the minimum weighted edge connected to vertices set { A, D, B}, and mark the other vertex on this edge as selected. A B C D

Demos For Finding MST Step 5: All vertex are marked as selected, So we find the minimum spanning tree A B C D

Pseudo code for Prim’s Alg. Minimum-Spanning-Tree-by-Prim(G, weight-function, source)  for each vertex u in graph G set key of u to ∞ set parent of u to nil  set key of source vertex to zero  enqueue all vertices to Q  while Q is not empty extract vertex u from Q // u is the vertex with the lowest key that is in Q for each adjacent vertex v of u do  if (v is still in Q) and (weight-function(u, v) < key of v) then  set u to be parent of v // in minimum-spanning-tree  update v's key to equal weight-function(u, v)

Demo for Prim’s Algorithm

Shortest Path Problem Weight: cost, distance, travel time, hop … u v

Single Source Shortest Path Problem Single source shortest path problem  Find the shortest path to all other nodes Dijkstra’s shortest path algorithm  Find shortest path greedily by updating distance to all other nodes

Example – Dijkstra Algorithm Greedy Algorithm Assume all weight of edge > source node from node V 0 to other nodes V1V1 10 V2V2 5 V3V3  V4V4  best

Example – Dijkstra Algorithm step 1: find the shortest path to node 0  node 2 is selected source node from node V 0 to other nodes V1V1 10 V2V2 5 V3V3  V4V4  bestV2V2

Example – Dijkstra Algorithm step 2: recalculate the path to all other nodes  find the shortest path to node 0. Node 4 is selected source node from node V 0 to other nodes V1V1 108 V2V2 55 V3V3  14 V4V4  7 bestV2V2 V4V4

Example – Dijkstra Algorithm step 3: recalculate the path to all other nodes  find the shortest path to node 0. node 1 is selected source node from node V 0 to other nodes V1V V2V2 555 V3V3  1413 V4V4  77 bestV2V2 V4V4 V1V1

Example – Dijkstra Algorithm step 3: recalculate the path to all other nodes  find the shortest path to node 0. node 2 is selected source node from node V 0 to other nodes V1V V2V V3V3  V4V4  777 bestV2V2 V4V4 V1V1 V3V3

Example – Dijkstra Algorithm Now we get all shortest paths to each node source node from node V 0 to other nodes V1V (0,2) 8 (0,2) 8 V2V2 5 (0,2) 555 V3V3  14 (0,2,3) 13 (0,2,4,3 ) 9 (0,2,1,3 ) V4V4  7 (0,2,4) 77 bestV2V2 V4V4 V1V1 V3V3

Dijkstra Algorithm Mark source node selected Initialize all distances to Infinite, source node distance to 0. Make source node the current node. While (there is unselected node) { Expand on current node Update distance for neighbors of current node Find an unselected node with smallest distance, and make it current node and mark this node selected }

Pseudo-code For Dijkstra’s Algorithm function Dijkstra(G, w, s) for each vertex v in V[G] // Initializations d[v] := infinity previous[v] := undefined d[s] := 0 S := empty set Q := V[G] while Q is not an empty set // The algorithm itself u := Extract_Min(Q) S := S union {u} for each edge (u,v) outgoing from u if d[u] + w(u,v) < d[v] // Relax (u,v) d[v] := d[u] + w(u,v) previous[v] := u

Demo for Dijkstra Algorithm