Example #1
0
graph* makeGraph(int v , int e){
  graph *temp = new graph;
  int i , j , k,sour , dest,wei,p;
  vertex *t;
  temp->noofedges = e;
  temp->noofvertex = v;
  temp->v = new vertex*[v];
  temp->e = new edge*[e];
  for(i = 0  ; i < v ; i ++){
    t = makeVertex(i);
    if(t == NULL){
      cout<<"Error Occured";

    }
    temp->v[i] = t;
  }
  cout<<"Enter 0 if weighted otherwise 1"<<endl;
  cin>>j;
  cout<<"Enter 0 if directed otherwise 1"<<endl;
  cin>>p;
  cout<<"Enter the source and destination of edges (between 0 and no. of the edge - 1):- "<<endl;
  for(i = 0 ; i < e; i++){
        cin>>sour>>dest;
        if(j)
          wei = 0;
        else
          cin>>wei;
        k = insertAdj(temp->v[sour] , temp->v[dest] , wei);
        if(p)
          k = insertAdj(temp->v[dest] , temp->v[sour] , wei);
        if(k)
            cout<<"Error in making adjacentylist  ";
        temp->e[i] = makeEdge(temp->v[sour] , temp->v[dest] , wei);

    }
	return temp;
  }
int main()
{ 
	AdjTable* adj;		
	Queue queue;
	int size = 7;
	int i;
	int j;
	int column = 4;
	int startVertex;

	int adjTable[7][4] = 
	{
		{2, 4, 0, 0},
		{4, 5, 0, 0},
		{1, 6, 0, 0},
		{3, 5, 6, 7},
		{7, 0, 0, 0},
		{0, 0, 0, 0},
		{6, 0, 0, 0}
	};
	
	printf("\n\n\t ====== test for topological sorting with adjoining table ======\n");
	adj = initAdjTable(size);	
	queue = initQueue(size);
	
	printf("\n\n\t ====== the initial adjoining table is as follows:======\n");
	for(i = 0; i < size; i++)
	 	for(j = 0; j < column; j++)	
			if(adjTable[i][j])			
				insertAdj(adj, adjTable[i][j]-1, i); // insertAdj the adjoining table over							 
	
	printAdjTable(adj, size);
	
	// finding the unweighted shortest path starts	
	// void unweighted_shortest_path(AdjTable* adj, int size, int initIndex, Queue queue)
	startVertex = 3; // we set the vertex3 as the start vertex (but you know, whose index in array is 2)
	unweighted_shortest_path(adj, size, startVertex, queue);
	
	return 0;
}