Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Please follow the link:https://www.dropbox.com/sh/75gwop7fcdapuid/AADeJNby2bctf-3VthhEYz-4a?dl=0 Details on the word doc.Thanks

1 answer below »
Please follow the link:https://www.dropbox.com/sh/75gwop7fcdapuid/AADeJNby2bctf-3VthhEYz-4a?dl=0
Details on the word doc.Thanks
Answered Same Day Nov 13, 2020

Solution

Snehil answered on Nov 16 2020
136 Votes
graph.cpp
graph.cpp
#include#include#include"graphmatrix.h"
#include"graphlist.h"
#include"Stack.h"
using namespace std;
void topologicalSort(GraphMatrix graphMatrix, int numVertices);
ool topologicalSortUtil(GraphMatrix graphMatrix, int numVertices, int v, int visited[], Stack &stack);
int main()
{
    string fileName;
    cout
"Enter file name : ";
    cin
fileName;
    cout
endl;
    ifstream fin(fileName);
    int numVertices;
    fin
numVertices;
    GraphMatrix graphMatrix(numVertices);
    GraphList graphList(numVertices);
    int u,v;
    fin
u
v;
    while(fin)
    {
        graphMatrix.addEdge(u,v);
        graphList.addEdge(u,v);
        fin
u
v;
    }
    graphMatrix.printGraph();
    cout
endl;
    graphList.printGraph();
    cout
endl;
    topologicalSort(graphMatrix, numVertices);
    return 0;
}
ool topologicalSortUtil(GraphMatrix graphMatrix,int numVertices, int v, int visited[], Stack &stack)
{
    bool isCyclic=false;
    visited[v] = 1;
    for(int i=0;i    {
        if(graphMatrix.isThereAnEdge(v,i))
        {
            if(visited[i]==0)
            {
                isCyclic = topologicalSortUtil(graphMatrix, numVertices, i, visited, stack);
            }
            else if(visited[i]==1)
            {
                isCyclic = true;
                
eak;
            }
        }
    }
    stack.push(v);
    visited[v]=2;
    return isCyclic;
}
void topologicalSort(GraphMatrix graphMatrix, int numVertices)
{
    int *visited = new int[numVertices];
    Stack  stack;
    int i;
    for(i=0;i    {
        if(visited[i]==0)
        {
            if(topologicalSortUtil(graphMatrix,numVertices,i,visited,stack)==true)
            {
                
eak;
            }
        }
        else if(visited[i]==1)
        {
            
eak;
        }
    }
    if(i==numVertices)
    {
        cout
"Topologically sorted graph vertices: ";
        while(stack.isEmpty()==false)
        {
            int val;
            stack.pop(val);
            cout
val
' ';
        }
    }
    else
    {
        cout
"Cannot sort topologically as the graph is Cyclic.";
    }
    delete [] visited;
}
graphlist.h
#ifndef GRAPHLIST_H
#define GRAPHLIST_H
using namespace std;
class GraphList
{
private:
struct ListNode
{
int val;
ListNode *next;
};
ListNode ** headA
ay;
int numVertices;
int numEdges;
public:
GraphList(int num)
{
numVertices = num;
headA
ay = new ListNode*[numVertices];
for(int i=0;i {
headA
ay[i] = new ListNode;
headA
ay[i]->val=i;
headA
ay[i]->next=NULL;
}
}
~GraphList()
{
for(int i=0;i {
ListNode* cur = headA
ay[i];
ListNode* next;
while(cur!=NULL)
{
next = cur->next;
delete cur;
cur=next;
}
}
delete [] headA
ay;
}
void addEdge(int u, int v)
{
ListNode* cur = headA
ay[u];
while(cur->next!=NULL)
{
cur = cur->next;
}
cur->next = new ListNode;
cur=cur->next;
cur->val=v;
cur->next=NULL;
numEdges++;
}
void printGraph()
{
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here