Professional Documents
Culture Documents
Combine Routing
Combine Routing
cpp Page 1 of 9
#include"bits/stdc++.h"
struct edge
{
int x;
int y;
int wt;
};
for(i=0;i<e;i++)
{
if(g[i].x == a && g[i].y == b)
{
return g[i].wt;
}
}
return 0;
}
graph->array[i].head = NULL;
return graph;
}
// A utility function to swap two nodes of min heap. Needed for min heapify
void swapMinHeapNode(struct MinHeapNode** a, struct MinHeapNode** b)
{
struct MinHeapNode* t = *a;
*a = *b;
*b = t;
}
if (smallest != idx)
{
// The nodes to be swapped in min heap
MinHeapNode *smallestNode = minHeap->array[smallest];
MinHeapNode *idxNode = minHeap->array[idx];
// Swap positions
minHeap->pos[smallestNode->v] = idx;
minHeap->pos[idxNode->v] = smallest;
// Swap nodes
swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
return root;
}
// The main function that calulates distances of shortest paths from src to all
// vertices. It is a O(ELogV) function
void d(struct Graph* graph, int src)
{
int V = graph->V;// Get the number of vertices in graph
int dist[V]; // dist values used to pick minimum weight edge in cut
// Initialize min heap with all vertices. dist value of all vertices
for (int v = 0; v < V; ++v)
{
dist[v] = INT_MAX;
minHeap->array[v] = newMinHeapNode(v, dist[v]);
minHeap->pos[v] = v;
}
void dijkstra()
{
int V = 9;
int i, v, e;
int from, to, w;
for(i=0;i<e;i++)
{
cin>>from>>to>>w;
d(graph, 0);
return;
}
int vt[v];
vt[0] = 0;
for(i=1;i<v;i++)
{
vt[i] = INT_MAX;
}
int w;
for(i=0;i<v-1;i++)
{
for(j=0;j<e;j++)
{
w = wt(g, e, g[j].x, g[j].y);
if(vt[g[j].y] > vt[g[j].x] + w)
{
vt[g[j].y] = vt[g[j].x] + w;
}
}
}
cout<<"The shorted path values from the starting node(0) to every other node : "<<endl;
for(i=0;i<v;i++)
{
File: /home/user/amb/cnlab/a6/combineRouting.cpp Page 6 of 9
void bellmanFord()
{
int i;
int v, e;
adj[g[i].x].push_back(g[i].y);
adj[g[i].y].push_back(g[i].x);
}
/* list<int>::iterator k;
for(i=0;i<v;i++)
{
cout<<i<<" -> ";
for(k=adj[i].begin();k!=adj[i].end();k++)
{
cout<<*k<<" ";
}
cout<<endl;
}
*/
bf(g, v, e);
return;
}
void distanceVector()
{
int graph[50][50];
int i,j,k,t;
int nn;
cout<<"Enter the number of nodes that have changed in the table of the surce node:"<<endl;
cin>>nn;
for (i=0;i<nn;i++)
{
for(j=0;j<nn;j++)
{
graph[i][j]=-1;
}
}
char ch[7]={'A','B','C','D','E','F','G'};
for (j=0;j<nn;j++)
{
for(i=0;i<nn;i++)
{
if(i==j)
{
graph[i][j]=0;
}
if(graph[i][j]==-1)
{
cout<<"Enter Distance between "<<ch[i]<<" - "<<ch[j]<<" : "<<endl;
cin>>t;
graph[i][j]=graph[j][i]=t;
}
File: /home/user/amb/cnlab/a6/combineRouting.cpp Page 7 of 9
}
}
int via[50][50];
for (i=0;i<nn;i++)
{
for(j=0;j<nn;j++)
{
via[i][j]=-1;
}
}
int sh[50][50][50];
for(i=0;i<nn;i++)
{
for(j=0;j<nn;j++)
{
for (k=0;k<nn;k++)
{
if((graph[i][j]>-1)&&(graph[j][k]>-1))
{
sh[i][j][k]=graph[j][k]+graph[i][j];
}
else
{
sh[i][j][k]=-1;
}
}
}
}
for(i=0;i<nn;i++)
{
cout<<"\n\n For "<<ch[i];
for (j=0;j<nn;j++)
{
cout<<"\n From "<<ch[j];
for(k=0;k<nn;k++)
{
cout<<"\n "<<ch[k]<<" "<<sh[i][j][k];
}
}
}
int final[50][50];
for(i=0;i<nn;i++)
{
for(j=0;j<nn;j++)
{
final[i][j]=graph[i][j];
via[i][j]=i;
for(k=0;k<nn;k++)
{
if(final[i][j]==-1)
{
for(k=0;k<nn;k++)
File: /home/user/amb/cnlab/a6/combineRouting.cpp Page 8 of 9
if((final[i][k]!=-1)&&(final[k][j]!=-1))
{
if((final[i][j]==-1) || ((final[i][j]!=-1) &&(final[i][j]>final[i][k]+final[k]
[j])))
{
if(final[i][k]+final[k][j]>-1)
{
final[i][j]=final[i][k]+final[k][j];
via[i][j]=k;
}
}
}
}
}
}
}
cout<<endl<<"After Initialization"<<endl;
for (i=0;i<nn;i++)
{
cout<<"\n"<<ch[i]<<" Table :";
cout<<"\nNode\tDist\tVia";
for(j=0;j<nn;j++)
{
cout<<"\n"<<ch[j]<<"\t"<<graph[i][j]<<"\t"<<via[i][j];
}
}
cout<<endl;
cout<<endl<<endl;
return;
}
switch(choice)
{
case 1 : dijkstra();
break;
case 2 : bellmanFord();
break;
case 3 : distanceVector();
break;
break;
}
return 0;
}