Download presentation
Presentation is loading. Please wait.
Published byDomenic Dalton Modified over 9 years ago
1
COSC 2007 Data Structures II Chapter 14 Graphs III
2
2 Topics Spanning trees DFS Spanning tree BFS Spanning tree Minimum spanning trees Algorithms Prim Dijkstra
3
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
4 Spanning Trees Example a f g e h d c b i
5
5 Spanning Trees Example Original graph 3 different spanning trees a dc ba dc ba dc ba dc b
6
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
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
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 6 7 5 4 3 21 8
9
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
10 Spanning Trees Minimum Spanning Tree (MST) Weighted connected, undirected graph a f g e h d c b i 8 4 9 7 2 4 6 1 2 5 3
11
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
12 Spanning Trees Minimum Spanning Tree (MST) Weighted connected, undirected graph a f g e h d c b i 8 4 9 7 2 4 6 1 2 5 3
13
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
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
15 Prim's Algorithm Minimum Spanning Tree (MST) a f g e h d c b i 8 4 9 7 2 4 6 1 2 5 3
16
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
17 Dijkstra's Algorithm A B C D E 30 60 100 10 50 10 Example: Cheapest (shortest-whatever) path from A to E? 20
18
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
19 Dijkstra's Algorithm A B C D E 30 60 100 10 50 10 What is the Minimum Spanning Tree? 20
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
21 Dijkstra's Algorithm Do this 20 10 30 40 100 50 30 N1 N2 N6 N3 N4 N5
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.