COSC 2007 Data Structures II Chapter 14 Graphs III.

Slides:



Advertisements
Similar presentations
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
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.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
CS202 - Fundamental Structures of Computer Science II
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.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
Spanning Trees.
Spanning Trees. 2 Spanning trees Suppose you have a connected undirected graph Connected: every node is reachable from every other node Undirected: edges.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Spanning Trees. Spanning trees Suppose you have a connected undirected graph –Connected: every node is reachable from every other node –Undirected: edges.
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.
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
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 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Minimum Spanning Trees
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
7.1 and 7.2: Spanning Trees. A network is a graph that is connected –The network must be a sub-graph of the original graph (its edges must come from the.
Module 5 – Networks and Decision Mathematics Chapter 23 – Undirected Graphs.
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.
Most of contents are provided by the website Graph Essentials TJTSD66: Advanced Topics in Social Media.
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.
COSC 2007 Data Structures II
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
Prims Algorithm for finding a minimum spanning tree
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Graph Search Applications, Minimum Spanning Tree
Minimum Spanning Trees
Graphs Representation, BFS, DFS
Minimum Spanning Tree Chapter 13.6.
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS202 - Fundamental Structures of Computer Science II
Short paths and spanning trees
Graph Algorithm.
Graphs Representation, BFS, DFS
Minimum Spanning Tree.
Minimum Spanning Trees
Shortest Path.
Spanning Trees.
Graphs.
Chapter 11 Graphs.
CSE 373: Data Structures and Algorithms
Weighted Graphs & Shortest Paths
CSC 380: Design and Analysis of Algorithms
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
GRAPH – Definitions A graph G = (V, E) consists of
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:

COSC 2007 Data Structures II Chapter 14 Graphs III

2 Topics Spanning trees DFS Spanning tree BFS Spanning tree Minimum spanning trees Algorithms Prim Dijkstra

3 Spanning Trees A special kind of undirected graph Contains all the vertex Is a tree There may be several spanning trees for a given graph If we have a connected undirected graph with cycle, we obtain a spanning tree by removing edges until we have no cycles We can determine whether a connected graph contains a cycle simply by counting its nodes & edges If we add an edge to a spanning tree, then the resulting graph is no longer a tree, because it will produce a cycle containing the new edge

4 Spanning Trees Example a f g e h d c b i

5 Spanning Trees Example Original graph 3 different spanning trees a dc ba dc ba dc ba dc b

6 Spanning Trees Observations about undirected graphs: A connected undirected graph that has N nodes must have at least N-1 edges A connected undirected graph that has N nodes and exactly N-1 edges contains no cycles A connected undirected graph that has N nodes & more than N-1 edges must contain at least one cycle Two algorithms for determining a spanning tree of a graph, based on traversal algorithms DFS BFS

7 Spanning Trees Spanning tree recursive (DFS) algorithm dfs_Tree_R(v) // Recursive version Mark v as visited for (each unvisited node u adjacent to v) {Mark the edge from u to v dfs_Tree_R(u) } // end for

8 Spanning Trees Spanning tree recursive (DFS) algorithm example Order of nodes visited a b c d g e f h i a f g e h d c b i Root

9 Spanning Trees Spanning tree iterative (BFS) algorithm Uses queue bfs_Tree_I( v) // Iterative version q.createQueue( ) // Add v to queue amd mark it q.enqueue(v) Mark v as visited while (!q.isEmpty ( ) ) {q.dequeue(w) for (each unvisited node u adjacent to w) {Mark u as visited Mark edge between w & u q.enqueue(u) } // end for } // end while

10 Spanning Trees Minimum Spanning Tree (MST) Weighted connected, undirected graph a f g e h d c b i

11 Spanning Trees Minimum Spanning Tree (MST) If a graph has weighted edges, it is called a Network The weight can be thought of as a cost or Distance Minimum spanning tree (MST): A subgraph of a given connected graph that is connected and that minimizes the sum of the weights of its edges If we have a graph G with weighted edges, then any spanning tree has associated weight which is equal to the sum of the weights of its edges

12 Spanning Trees Minimum Spanning Tree (MST) Weighted connected, undirected graph a f g e h d c b i

13 Prim's Algorithm Building a MST of an undirected graph Idea: Choose the root for the MST Start with the MST having the root node Repeatedly add to MST the graph node that is closest, in the sense of having least weight, to MST Continue until all nodes have been added At the end of each pass, the marked tree is a MST for that subgraph of G consisting of the nodes chosen so far, along with all edges of G connecting these nodes.

14 Prim's Algorithm Prim”s Algorithm(v) // Determines a MST for a weighted (non-negative), //connected, undirected graph, beginning with node v Mark node v as visited & include it in the MST while ( there are unvisited nodes) {Find the least cost edge (v, u) to the MST Mark u as visited Add node u & the edge (v, u) to the MST } // end while

15 Prim's Algorithm Minimum Spanning Tree (MST) a f g e h d c b i

16 Dijkstra's Algorithm One of the greedy algorithms At each step the best "local" solution to the problem is the one chosen Finds the shortest (cheapest) path between two nodes in a weighted directed graph Known as “single-source least-cost” problem

17 Dijkstra's Algorithm A B C D E Example: Cheapest (shortest-whatever) path from A to E? 20

18 Dijkstra's Algorithm For each vertex keep track of Cost Cheapest known cost to get this vertex from the source Path The path with cheapest known cost to get this vertex from the source Done Is the path and cost guaranteed to be optimum?

19 Dijkstra's Algorithm A B C D E What is the Minimum Spanning Tree? 20

Dijkstra's Algorithm Set cost of source vertex to 0 While destination vertex no done do Find cheapest not Done vertex (X) Mark it as done It is guaranteed to be so For each vertex X do Calculate total cost of getting to vertex If total cost < current cost Replace current cost with lowest cost Mark route (path) as X

21 Dijkstra's Algorithm Do this N1 N2 N6 N3 N4 N5