Vous êtes sur la page 1sur 4

Week 9: Graphs

Kruskals and Prims algorithms for constructing minimum spanning trees for
weighted diagrams.
Kruskals is fairly intuitive (same as the intuition to construct a minimum
spanning tree):
1. sort all the edges by weighting (weight order)
2. loop and take off next edge in array, stick it on the graph
if it doesnt form a cycle, keep the edge
3. repeat step 2 until all nodes connected
N nodes gives N-1 edges for a minimum spanning tree - all we need
to terminate
is to increment a counter.
could also just go through the entire array though
Prims isnt as intuitive but still fairly intuitive:
1. start at some node
Concept of a fringe: the set of edges you can reach next from ANY of
the nodes
we have already visited.
2. pick the lowest cost edge thats adjacent ANY of the nodes we have
already visited,
add it to the spanning tree
if the fringe edge you picked goes back to a node you already
visited, dont
add it to the tree.
1. For a weighted graph
Adjacency matrix: instead of a true / false (1 / 0), the value is the weighting.
0
0
1
2
3
4
5

1
2

5
4
3
5
2
1

If the weighting is 0 or negative weights, need some other constant to indicate no


edge exists.

Adjacency list: an array of (an array of structs: contains node value and weight)
struct edge {
int vertex;
int weight;
struct node* next;
}

2. Spanning Trees
If theres a cycle, its not a tree.
Minimum spanning tree: accumulative weight for the nodes is the smallest you can
get.
I.e. you need to ignore all the highest weighted edges.
E.g. connecting houses to an electricity network - create a minimum spanning tree to
save money
1. create a clique (or less)
2. delete all the most expensive edges until you get a tree
3. Minimum spanning trees:
Intuitively we just get rid of
4. Kruskals algorithm:
1. minimum spanning tree: have an empty graph
2. construct and sort the array of edges
3. note the for loop: checks the number of edges in the graph is less than:
number of vertices -1
i.e. you need n-1 edges to form a spanning tree
you only look at exactly n-1 edges in the graph has no cycles
maximum number of edges to consider: all of them in the graph
Add another edge to force the algorithm to the worst case:
1. i.e. the edge you need is at the end of the array
2. modify the graph so that there is one node sticking out, connected only by 1
edge
to make it be considered last: have the largest weight (e.g. 11 in this case)

5.
adjacency matrix
function that takes a graph and returns a sorted array of edges (we write into
edges[]):
void sortedEdgeList (Graph g, Edge edges[]) {
// assume edges[] has enough space to write into
//for (int i = 0; i < g.nE; i++) {

// adjacency list version

// matrix: need 2 loops (2D)


int edgePos = 0;
for (int i = 0; i < g.nV; i++) {
for (int j = i +1; j < g.nV; j++) { // consider only the upper triangle of the
matrix (no dups)
if (g.edges[i][j] != 0) {
edges[edgePos].s = i;
// from - the rows (id number for the
node)
edges[edgePos].t = j;
// to - the cols (id number for the
node)
edges[edgePos].w = g.edges[i][j];
// weight (the value in the
matrix)
edgePos++;
}
}
}
qsort(edges, g.nE, other arguments);
// lazy solution, not efficient (in
stdlib.h)
}
6. Prims:
Again, need an MSTree we add edges on to.
Set of vertexes: initially contains just one vertex
Set of fringes: initially empty
// (cardinality means the number of things in it)
while cardinality of the vertex set is less than the number of vertexes that exist
find the edge in fringe with minimum cost
extract the start, end, weight info from that edge (a struct probably)

add the vertex and edge to their corresponding sets


// clean up steps
once youve added the vertex to the set, add all the new fringe edges to
the set
in here, do a check so that we dont add any fringe edges that go
back to a vertex
we have already visited.
another optimisation: check the new fringe edge goes to the same vertex
as an old fringe
edge - if so, get rid of that old fringe edge, and only use the new fringe
edge
// i.e. (if weight of edge (common to vertex) > weight of our new edge)
//
get rid of that old edge, put in the new edge
// otherwise:
//
get rid of our new edge, put in the old edge
Takes a while to write up the C version of this.
7. another minimum spanning tree algorithm (from Sedgewick)

Vous aimerez peut-être aussi