0% found this document useful (0 votes)
23 views32 pages

Week 8 - Greedy Algorithm 2

nothing

Uploaded by

abidformuli100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views32 pages

Week 8 - Greedy Algorithm 2

nothing

Uploaded by

abidformuli100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Greedy Algorithms

Minimum Spanning Trees


 MST is another example of greedy algorithms.
 Remove any extra traces.
 The result would be a graph with the minimum
number of edges necessary to connect the vertices.
 There are many possible MST.
 E=V–1
 The Algorithm for creating MST can be based on
either the DFS or the BFS.
 By executing the DFS and recording the edges you
have traveled to make the search, you automatically
create a minimum spanning tree.
 The only difference between the mst() and dfs() is
that mst() must somehow record the edges
traveled.
public void mst() {
vertexlist[0].wasvisited = true;
thestack.push(0);
while ( !thestack.isempty() ) {
int currentvertex = thestack.peek();
int v = getadjunvisitedvertex(currentvertex);
if(v == -1)
thestack.pop();
else {
vertexlist[v].wasvisited = true;
thestack.push(v);
displayvertex(currentvertex);
displayvertex(v);
System.out.print(" ");
}}
for(int j=0; j<nverts; j++)
vertexlist[j].wasvisited = false;}
class mststack {
private final int size = 20;
private int[] st;
private int top;
public mststack() {
st = new int[size];
top = -1;
}
public void push (int j) {
st[++top] = j;
}
public int pop() {
return st[top--];
}
public int peek() {
return st[top];
}
public boolean isempty() {
return (top == -1);
}
}
class vertex{
public char label;
public boolean wasvisited;
public vertex(char lab){
label = lab;
wasvisited = false;
}
}
class mstgraph {
private final int max_verts = 20;
private vertex vertexlist[];
private int adjmat[][];
private int nverts;
private mststack thestack;
public mstgraph() {
vertexlist = new vertex[max_verts];
adjmat = new int[max_verts][max_verts];
nverts = 0;
for(int j=0; j<max_verts; j++)
for(int k= 0; k<max_verts; k++)
adjmat[j][k] = 0;
thestack = new mststack();
}
public void addvertex(char lab){
vertexlist[nverts++] = new vertex(lab);
}
public void addedge(int start, int end){
adjmat[start][end] = 1;
adjmat[end][start] = 1;
}
public void displayvertex(int v){
System.out.print(vertexlist[v].label);
}
public void mst() {
vertexlist[0].wasvisited = true;
thestack.push(0);
while ( !thestack.isempty() ) {
int currentvertex = thestack.peek();
int v = getadjunvisitedvertex(currentvertex);
if(v == -1)
thestack.pop();
else {
vertexlist[v].wasvisited = true;
thestack.push(v);
displayvertex(currentvertex);
displayvertex(v);
System.out.print(" ");
}}
for(int j=0; j<nverts; j++)
vertexlist[j].wasvisited = false;}
public int getadjunvisitedvertex(int v){
for(int j=0; j<nverts; j++)
if(adjmat[v][j] == 1 && vertexlist[j].wasvisited == false)
return j;
return -1;
}
}
class mstapp {
public static void main (String[] args) {
mstgraph thegraph = new mstgraph();
thegraph.addvertex('A');
thegraph.addvertex('B');
thegraph.addvertex('C');
thegraph.addvertex('D');
thegraph.addvertex('E');
thegraph.addedge(0, 1);
thegraph.addedge(0, 2);
thegraph.addedge(0, 3);
thegraph.addedge(0, 4);
thegraph.addedge(1, 2);
thegraph.addedge(1, 3);
thegraph.addedge(1, 4);
thegraph.addedge(2, 3);
thegraph.addedge(2, 4);
thegraph.addedge(3, 4);
System.out.print("Minimum Spanning Tree: ");
thegraph.mst();
System.out.println();
}
}
Weighted Graphs
 Edges have weight.
 With weighted graph some interesting and

complex questions arise.


 What is the minimum spanning tree for a

weighted graph?
 What is the shortest distance from one

vertex to another?
Minimum Spanning Tree with
Weighted Graphs
 The key activity is to maintain a list of the costs of
links between pairs of nodes.
 In this list we repeatedly select the minimum
value.
 Priority queue is an appropriate data structure.
Outline of the Algorithm
 Start with a vertex, and put it in the tree. Then
repeatedly do the following:
 Find all the edges from the newest vertex to

other vertices that are not in the tree.


 Put these edges in the priority queue.

 Pick the edge with the lowest weight, and add

this edge and its destination vertex to the tree.


 Repeat these steps until all the vertices are in the

tree.
 During the execution of the Algorithm we make
sure that we do not have any edges in the priority
queue that lead to vertices that are already in the
tree.
 We could go through the queue looking for and
removing any such edges each time we added a
new vertex to the tree.
 The following method creates the minimum
spanning tree for a weighted graph, follows the
algorithm outlined earlier.
public void mstw() {
currentvertex = 0;
while(ntree < nverts – 1) {
vertexlist[currentvertex] .isintree = true;
ntree++;
for(int j = 0; j<nverts; j++){
if(j == currentvertex)
Continue;
if(vertexlist[j].isintree)
continue;
int distance = adjmat[currentvertex][j];
if(distance == infinity)
continue;
putinpq(j, distance);
}
if(thepq.size() == 0){
System.out.println(“Graph not connected”);
return;
}
edge theedge = thepq.removemin();
int sourcevert = the edge.srcvert;
currentvert = theedge.destvert;
System.out.print( vertexlist[sourcevert].abel);
System.out.print(vertexlist[currentvertex].label);
System.out.print(“ “);
}
for(int j=0; j<nverts; j++)
vertexlist[j].isintree = false;
}
 The algorithm is carried out in the while loop, which
terminate when all vertices are in the tree.
 Within this loop the following activities take place:
 The current vertex is placed in the tree.

 The edges adjacent to this vertex are placed in the

priority queue.
 Edge with the minimum weight is removed from the

priority queue.
 The destination vertex of this edge becomes the current

vertex.
 The following is the code for putinpq() method:
public void putinpq(int newvert, int newdist) {
int queueindex = thepq.find(newvert);
if(queueindex != -1) {
edge tempedge = thepq.peekn(queueindex);
int olddist = tempedge.distance;
if(olddist > newdist){
thepq.removen(queueindex);
edge theedge = new edge(currentvertex, newvert, newdist);
thepq.insert(theedge);
}
}
else
{
edge theedge = new edge(currentvertex, newvert, newdist);
thepq.insert(theedge);
}}
End of Chapter 8

Any Question?

You might also like