





























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Some concept of Data Structures and Algorithm are Permutation, Representation, Implemented, Algorithm Design, Dynamic Programming, Graph Data Structures, String Processing, General Trees. Main points of this lecture are: Minimum Spanning Trees, Arrange, Rambunctious, Children, Straight, Statements, Capable, Throwing, Minimum Number, Efficient Algorithm
Typology: Slides
1 / 37
This page cannot be seen from the preview
Don't miss anything!






























Your job is to arrange n rambunctious children in a straight line, facing front. You are given a list of m statements of the form “i hates j”. If i hates j, then you do not want put i somewhere behind j, because then i is capable of throwing something at j.
Beyond DFS/BFS exists an alternate universe of algorithms for edge-weighted graphs. Our adjancency list representation quietly supported these graphs:
typedef struct { int y; int weight; struct edgenode *next; } edgenode;
A tree is a connected graph with no cycles. A spanning tree is a subgraph of G which has the same set of vertices of G and is a tree. A minimum spanning tree of a weighted graph G is the spanning tree of G whose edges sum to minimum weight. There can be more than one minimum spanning tree in a graph → consider a graph with identical weight edges.
The minimum spanning tree problem has a long history – the first algorithm dates back at least to 1926!. Minimum spanning tree is always taught in algorithm courses since (1) it arises in many applications, (2) it is an important example where greedy algorithms always give the optimal answer, and (3) Clever data structures are necessary to make it work. In greedy algorithms, we make the decision of what next to do by selecting the best local option from all available choices
Minimum spanning trees are useful in constructing networks, by describing the way to connect a set of sites using the smallest total amount of wire. Minimum spanning trees provide a reasonable way for clustering points in space into natural groups. What are natural clusters in the friendship graph?
When the cities are points in the Euclidean plane, the minimum spanning tree provides a good heuristic for traveling salesman problems. The optimum traveling salesman tour is at most twice the length of the minimum spanning tree.
The Option Traveling System tour is at most twicethe length of the minimum spanning tree. Note: There can be more than one minimum spanningtree considered as a group with identical weight edges.
If G is connected, every vertex will appear in the minimum spanning tree. If not, we can talk about a minimum spanning forest. Prim’s algorithm starts from one vertex and grows the rest of the tree an edge at a time. As a greedy algorithm, which edge should we pick? The cheapest edge with which can grow the tree by one vertex without creating a cycle.
6
G Prim(G,A) Kruskal(G)
3
A
2 3 4
1 5 A
4 2 6
5 1 A
2
5 (^43) 7 12
7
4 7
2 9 5
We use a proof by contradiction: Suppose Prim’s algorithm does not always give the minimum cost spanning tree on some graph. If so, there is a graph on which it fails. And if so, there must be a first edge (x, y) Prim adds such that the partial tree V ′^ cannot be extended into a minimum spanning tree.
Thus we cannot go wrong with the greedy strategy the way we could with the traveling salesman problem.
That depends on what data structures are used. In the simplest implementation, we can simply mark each vertex as tree and non-tree and search always from scratch:
Select an arbitrary vertex to start. While (there are non-tree vertices) select minimum weight edge between tree and fringe add the selected edge and vertex to the tree
This can be done in O(nm) time, by doing a DFS or BFS to loop through all edges, with a constant time test per edge, and a total of n iterations.
v = start; while (intree[v] == FALSE) { intree[v] = TRUE; p = g− >edges[v]; while (p! = NULL) { w = p− >y; weight = p− >weight; if ((distance[w] > weight) && (intree[w] == FALSE)) { distance[w] = weight; parent[w] = v; } p = p− >next; } v = 1; dist = MAXINT; for (i=1; i<= g− >nvertices; i++) if ((intree[i] == FALSE) && (dist > distance[i])) { dist = distance[i]; v = i; } } }
Finding the minimum weight fringe-edge takes O(n) time – just bump through fringe list. After adding a vertex to the tree, running through its adjacency list to update the cost of adding fringe vertices (there may be a cheaper way through the new vertex) can be done in O(n) time. Total time is O(n^2 ).