Spanning Tree A spanning tree is any tree that consists solely of edges in G and that includes all the vertices in G. Example.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Spanning Trees
Advertisements

Lecture 15. Graph Algorithms
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Shortest paths and transitive closure Data structure 2002/12/4.
Minimum cost spanning tree and activity networks Data structure 2002/12/2.
3.3 Spanning Trees Tucker, Applied Combinatorics, Section 3.3, by Patti Bodkin and Tamsen Hunter.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
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.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
1 Pertemuan 13 Minimum Spanning Tree (MST) Matakuliah: T0534/Struktur Data Tahun: 2005 Versi: September 2005.
Is the following graph Hamiltonian- connected from vertex v? a). Yes b). No c). I have absolutely no idea v.
GRAPHS Education is what remains after one has forgotten what one has learned in school. Albert Einstein Albert Einstein Smitha N Pai.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 2 Graph Algorithms.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Spanning Trees Introduction to Spanning Trees AQR MRS. BANKS Original Source: Prof. Roger Crawfis from Ohio State University.
Spanning Trees Introduction to Spanning Trees AQR MRS. BANKS Original Source: Prof. Roger Crawfis from Ohio State University.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
Module 5 – Networks and Decision Mathematics Chapter 23 – Undirected Graphs.
Minimum spanning trees Aims: To know the terms: tree, spanning tree, minimum spanning tree. To understand that a minimum spanning tree connects a network.
Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free.
Graphs 2015, Fall Pusan National University Ki-Joune Li.
Minimum Spanning Tree What is 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.
Unit 11 Graphs (2) King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Graphs Chapter 20.
Minimum Spanning Trees
Minimum Spanning Tree What is a Minimum Spanning Tree.
Minimum Spanning Tree Chapter 13.6.
Discrete Mathematicsq
Minimal Spanning Trees
C.Eng 213 Data Structures Graphs Fall Section 3.
Shortest Path Graph represents highway system Edges have weights
CS120 Graphs.
The Greedy Approach Winter-2004 Young CS 331 D&A of Algo. Greedy.
Short paths and spanning trees
Autumn 2016 Lecture 11 Minimum Spanning Trees (Part II)
Graph Algorithm.
Minimum-Cost Spanning Tree
Minimum Spanning Trees
Connected Components Minimum Spanning Tree
Minimum Spanning Tree.
Graphs Chapter 13.
Autumn 2015 Lecture 11 Minimum Spanning Trees (Part II)
CSE 373 Data Structures and Algorithms
Spanning Trees.
Minimum-Cost Spanning Tree
2018, Fall Pusan National University Ki-Joune Li
Minimum-Cost Spanning Tree
CS 583 Analysis of Algorithms
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
Chapter 11 Graphs.
2017, Fall Pusan National University Ki-Joune Li
Minimum Spanning Tree Algorithms
CSCI2100 Data Structures Tutorial
CSE 373: Data Structures and Algorithms
Kruskal’s Idea Adding one edge at a time Non-decreasing order No cycle
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Winter 2019 Lecture 11 Minimum Spanning Trees (Part II)
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Algorithm Course Dr. Aref Rashad
Minimum-Cost Spanning Tree
Autumn 2019 Lecture 11 Minimum Spanning Trees (Part II)
Presentation transcript:

Spanning Tree A spanning tree is any tree that consists solely of edges in G and that includes all the vertices in G. Example

Constructing Spanning Trees Any traversal of a connected, undirected graph visits all the vertices in that graph. The set of edges which are traversed during a traversal forms a spanning tree. For example, Fig:(b) shows the spanning tree obtained from a breadth-first traversal starting at vertex b. Similarly, Fig:(c) shows the spanning tree obtained from a depth-first traversal starting at vertex c. (a) Graph G (b) Breadth-first spanning tree of G rooted at b (c) Depth-first spanning tree of G rooted at c

Minimum cost spanning tree The cost of the spanning tree of a weighted undirected graph is the sum of the costs of the edges in the spanning tree. A minimum cost spanning tree is a spanning tree of least cost. Three algorithms are used to find this All these assume about the solution following constraints

We must use only edges of the graph We must have exactly n-1 edges We may not use edges that would produce a cycle.

Example Minimum-Cost Spanning Tree For an edge-weighted , connected, undirected graph, G, the total cost of G is the sum of the weights on all its edges. A minimum-cost spanning tree for G is a minimum spanning tree of G that has the least total cost. Example: The graph Has 16 spanning trees. Some are: The graph has two minimum-cost spanning trees, each with a cost of 6:

Applications of Minimum-Cost Spanning Trees Minimum-cost spanning trees have many applications. Some are: Building cable networks that join n locations with minimum cost. Building a road network that joins n cities with minimum cost. Obtaining an independent set of circuit equations for an electrical network. In pattern recognition minimal spanning trees can be used to find noisy pixels.

Kruskal's Algorithm. Kruskal’s algorithm finds the minimum cost spanning tree of a graph by adding edges one-by-one. Algorithm: T={} While(T contains less than n-1 edges &&E is not empty) { Choose a least cost edge (v,w) from E Delete (v,w) from E If((v,w) does not create a cycle in T) Add(v,w) to T Else discard (v,w) } If (T contains fewer than n-1 edges) print no spanning tree.

Example for Kruskal’s Algorithm. Trace Kruskal's algorithm in finding a minimum-cost spanning tree for the undirected, weighted graph given below: The minimum cost is: 24

Prim’s Algorithm Prim’s algorithm finds a minimum cost spanning tree by selecting edges from the graph one-by-one as follows: It starts with a tree, T, consisting of the starting vertex, x. Then, it adds the shortest edge emanating from x that connects T to the rest of the graph. It then moves to the added vertex and repeats the process. Algorithm: T={} TV={0} /*start with vertex 0 and no edges*/ While(T contains fewer than n-1 edges) { let (u,v) be a least cost edge such that u is in set TV and v is not in TV; If(there is no such edge) break; Add v to TV; Add(u,v) to T; } If T contains fewer than n-1 edges print “no spanning tree”.

Example Trace Prim’s algorithm starting at vertex a: The resulting minimum-cost spanning tree is:

Shortest Path algortithms

Dijikstra’s Algorithm #define MAX_VERTICES 6 int cost[][MAX_VERTICES]={ {0,50,10,1000,45,1000}, {1000,0,15,1000,10,1000}, {20,1000,0,15,1000,1000}, {1000,20,1000,0,35,1000}, {1000,1000,30,1000,0,1000}, {1000,1000,1000,3,1000,0}};

int dist[MAX_VERTICES]; short int found[MAX_VERTICES]={0}; int n=MAX_VERTICES; int choose(int dist[],int n,short int found[]) { int i,min,minpos; min=32767; minpos=-1; for(i=0;i<n;i++){ If(dist[i]<min && !found[i]){ min=dist[i]; minpos=i;} return minpos;}

void shortestpath(int v,int cost[][MAX_VERTICES],int dist[],int n,short int found){ /*dist[i] represents shortest path from vertex v to i.found[i] holds 0 if shortest path to vertex i has not been found 1 otherwise.*/ int i,u,w; for(i=0;i<n;i++){ dist[i]=cost[v][i];} found[v]=1;dist[v]=0;

for(i=0;i<n-2;i++){ u=choose(dist,n,found); found[u]=1; for(w=0;w<n;w++) if(!found[w]) if(dist[u]+cost[u][w]<dist[w]) dist[w]=dist[u]+cost[u][w]; }

found dist 50 10 1000 45 u -

found 0 1 dist 50 10 1000 45 u -

found 0 1 0 1 dist 50 10 1000 25 45 1000 u 2

found 0 1 0 1 0 1 dist 50 45 10 1000 25 45 1000 u 3

found 0 1 0 1 0 1 dist 50 45 10 1000 25 45 1000 u 1

found 0 1 0 1 0 1 dist 50 45 10 1000 25 45 1000 u 4

All pairs Shortest Path algorithm void allcosts(int cost[][MAX_VERTICES],int dist[][MAX_VERTICES],int n){ /* determine the distances from each vertex to every other vertex, cost is the adjacency matrix ,dist is the matrix of distances*/ int i,j,k; for(i=0;i<n;i++) for(j=0;j<n;j++) {dist[i][j]=cost[i][j];}

for(k=0;k<n;k++) for(i=0;i<n;i++) for(j=0;j<n;j++) if(dist[i][k]+dist[k][j]<dist[i][j]) Dist[i][j]=dist[i][k]+dist[k][j]; }

The basic idea with the all pairs algorithm is to begin with the matrix A-1 and successively A0,A1,A2,A3……An-1. If we have already generated Ak-1 then we may generate Ak as following rule. The shortest path from I to j going through no vertex with index greater than k does not go through the vertex with index k and so its cost in Ak-1[i][j]. The shortest such path does go through vertex k.Such a path consists of a path from I to k followed by one from k to j.Neither of these goes through a vertex with index greater than k-1. Hence their cost are Ak-1[i][k] and Ak-1[k][j].

Ak[i][j]=min{Ak-1[i][j],Ak-1[i][k]+Ak-1[k][j]},k>=0 A-1[i][j]=cost[i][j].

A-1 1 2 4 11 6 3 - A0 1 2 4 11 6 3 7 A2 1 2 4 6 5 3 7 A1 1 2 4 6 3 7

Transitive closure