Exp
10
Prim’s
algo
g[10][10]
|
3 2
1
8
4
2
|
|
Visited
|
|
Cost array changes
|
|
Parent
|
|
0
|
3
|
2
|
999
|
|
||
|
3
|
0
|
999
|
1
|
|
||
|
2
|
999
|
0
|
999
|
4
|
||
|
999
|
1
|
999
|
0
|
|
||
|
8
|
999
|
4
|
2
|
|
|
4
|
|
3
|
i/p graph simi. to Kruskal’s algo
Class graph
{
char city[20][20]; //i/p city names in this array
int g[10][10]; // i/p weights in this array -
city[0] is name of vertex g[0]
public :
graph();
void
create();
void display();
void prim();
}
Graph()
{
i/p no. of cities &
initialize g[20][20] to value 9999;
//max value
also i/p cities in city array
}
Create()
{
//directed or undirected
optional
i=0; …// index of edges
// input the edges
Do
{
i/p source city sc;
int sc=search(source city
name); //returns index of city
i/p destination city dc;
int dest=serach(dc);
Enter cost of edges;
Cin>>g[sc][dest];
for undirected graph copy
g[dest][source]=g[source][dest];
want to continue? y/n
}while (ch==’y’);
}
Void display()
{
Display the matrix (with city
names if possible)
}
int minCost(int cost[],
int visited[])……………………..//returns min cost vertex
{
//
Initialize min value
int min
= 9999, min_index;
for (int
v = 0; v < V; v++)
if
(visited[v] == 0 && cost[v] < min)
min
= cost[v], min_index = v;
return min_index;
}
// function to print the
constructed MST stored in parent[]
int printMST(int parent[],
int n, int graph[V][V])
{
printf("Edge
Weight\n");
for (int
i = 1; i < V; i++)
printf("%d
- %d %d \n", parent[i], i, graph[i][parent[i]]);
}
// Function to construct
MST for a graph represented using adjacency
// matrix representation
void Prim(int graph[V][V] )
{
int
parent[V]; // Array to store constructed MST
int
cost[V]; // Key values used to pick minimum weight edge in cut
int
visited[V]; // To represent set of vertices not yet included in MST
|
Initialize cost array to
9999
Visited array to 0
0 is start vertex so
don’t have parent
Cost of 0 vertex is
minimum in cost array so
1st run:
u=0 check all g[0][v] , cost[1]=3
cost[2]=2
2nd run: u=2 check all g[2][v], cost[4]=4
3rd run : u=4
check all g[4][v], cost[3]=2
4th run : u=3 check all g[3][v], cost[1]=1
|
for
(int i = 0; i < V; i++)
{cost[i]
= 9999, visited[i] = 0;}
//
Always include first 1st vertex in MST.
cost[0]
= 0; // Make key 0 so that this vertex is picked as
first vertex
parent[0]
= -1; // First node is always root of MST
//
The MST will have V vertices
for
(int count = 0; count < V-1; count++)
{
//
Pick the minimum cost vertex from the set of vertices
//
not yet included in MST
int
u = mincost(cost, visited);
//
Add the picked vertex to the MST Set
visited[u]
= 1;
//
Update cost value and parent index of the adjacent vertices of
//
the picked vertex. Consider only those vertices which are not yet
//
included in MST
for
(int v = 0; v < V; v++)
//
visited[v] is false(zero) for vertices not yet included in MST
//
Update the cost only if graph[u][v] is smaller than cost[v]
if
(graph[u][v] && visited[v] == 0 && graph[u][v] < cost[v])
{ parent[v]
= u,
cost[v] = graph[u][v];
}
}
//
print the constructed MST
printMST(parent,
V, graph);
}
No comments:
Post a Comment