-2

I am solving a graph problem where I have to take input from a file. Below is my input.txt file.

12
1 2
2 3
2 4
2 5
3 6
4 5
4 7
5 2
5 6
5 7
6 3
6 8
7 8
7 10
8 7
9 7
10 9
10 11
11 12
12 10

In the above input.txt file first input is no of vertices and the others till the end of the file are directed edge of Graph. The first one is the source and the second one is the destination. All the input will be read from the input.txt file.

#include <bits/stdc++.h>
#include <fstream>
using namespace std;

class Graph {
    private:        
        int V;
        list<int> *l;

    public:
        Graph(int V) {
            this->V = V;
            l = new list<int>[V];
        }

        void addEdge(int source, int destination) {
            // As it is a directed graph edge will be source to destination only
            l[source].push_back(destination);
        }

        void printAdjList() {
            for(int i = 1; i <= V; i++) {
                cout << "Vertex " << i << "-> " ;
                for(int previous: l[i]) {
                    cout << previous << " " ;
                }
                cout << endl;
            }
        }
};



int main() {
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    ifstream inputFile;
    inputFile.open("input.txt");

    int noOfVertex, s, d, noOfEdge=20;
    inputFile >> noOfVertex ;
    // cout << noOfVertex << endl;

    Graph g(noOfEdge);

    // while(cin.eof()) {
    //     // cout << s << " " << d << endl;
    //     cin >> s >> d;
    //     g.addEdge(s, d);
    // }

    if(inputFile) {
        while(inputFile >> s >> d) {
            // cout << s << " " << d << endl;
            g.addEdge(s, d);
        }

        inputFile.close();
    }
    else {
        cout << "Error opening input file" << endl;
    }

    g.printAdjList();

    return 0;
}

I am getting this result after running the code

Vertex 1-> 2 
Vertex 2-> 3 4 5 
Vertex 3-> 6 
Vertex 4-> 5 7 
Vertex 5-> 2 6 7 
Vertex 6-> 3 8 
Vertex 7-> 8 10 
Vertex 8-> 7 
Vertex 9-> 7 
Vertex 10-> 9 11 
Vertex 11-> 12 
Vertex 12-> 10 
Vertex 13-> 
Vertex 14-> 
Vertex 15-> 
Vertex 16-> 
Vertex 17-> 
Vertex 18-> 
Vertex 19-> 

I can not take the number of edge for this problem. The number of vetices and given directed edges will be taken one by one line from the file and it will show an adjacency list like that

Vertex 1-> 2 
Vertex 2-> 3 4 5 
Vertex 3-> 6 
Vertex 4-> 5 7 
Vertex 5-> 2 6 7 
Vertex 6-> 3 8 
Vertex 7-> 8 10 
Vertex 8-> 7 
Vertex 9-> 7 
Vertex 10-> 9 11 
Vertex 11-> 12 
Vertex 12-> 10 

How can I take input from the file so that I can get the above output? I have applied many methods but nothing is working.

  • Your Graph constructor takes a number of nodes as a parameter. Why are you passing the number of edges? – Sneftel May 12 '21 at 15:42
  • 1
    Unrelated: `#include ` suggests you don't know what `#include ` does. Here's some reading on that along with a not-so-subtle suggestion: [Why should I not #include ?](https://stackoverflow.com/questions/31816095) – user4581301 May 12 '21 at 15:50
  • Hardcoding `noOfEdge` will only lead to pain and suffering. Fortunately it doesn't do anything useful and you can remove it. – user4581301 May 12 '21 at 15:52
  • Not directly related to your question, but are you aware that array indices start at **0** in C language? – Serge Ballesta May 12 '21 at 16:09

2 Answers2

1

I suggest you make your addEdge method more flexible and easier to use. As one problem, if the source input is larger than the size of list your program will crash. The logic should be:

if there is no source vertex, add it
if there is no destination vertex, add it
add link from source to destination.

Here is a more detailed description of the suggested procedure.

/// find vertex "n", returning vertex index, or -1 if missing
 int find( int n )
{
  loop over graph
     if vertex is "n
        return index
  return -1
}

/// find vertex "n", or add it if not present
int findoradd( int n )
{
   int i = find( n );
   if( i >= 0 )
      return i
   return addvertex( n )
}

/// add link between vertices
void addLink( int u, int v )
{
    addEdge(
       findoradd( u ),
       findoradd( v ) );
}
ravenspoint
  • 19,093
  • 6
  • 57
  • 103
1

Your code is wrong for 2 reasons:

  • you use a hardcoded number of edges when should use the number of vertex
  • you use an array with an index starting at 0 when the number of the vertex is read from file and does not start at 0

If you want to be safe, you should use a map (or unordered map) int -> list:

...
class Graph {
private:
    int V;
    unordered_map<int, list<int> > l;   // change here

public:
    Graph(int V) {
        this->V = V;
        l.reserve(V);                   // here
    }
...
    int noOfVertex, s, d, noOfEdge = 20;
    inputFile >> noOfVertex;
    // cout << noOfVertex << endl;

    Graph g(noOfVertex);                // and here
...

That is enough to get as expected:

Vertex 1-> 2
Vertex 2-> 3 4 5
Vertex 3-> 6
Vertex 4-> 5 7
Vertex 5-> 2 6 7
Vertex 6-> 3 8
Vertex 7-> 8 10
Vertex 8-> 7
Vertex 9-> 7
Vertex 10-> 9 11
Vertex 11-> 12
Vertex 12-> 10
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252