-3

is there a way to read inputs from txt file

6
0 1 1
2 3 1
1 2 1
3 0 1
4 0 1
4 5 1
3 4 1
5 3 1

for the part

int V = 6;
    Graph g(V);

    g.insertEdge(0,1,1);
        g.insertEdge(2,3,1);
        g.insertEdge(1,2,1);
        g.insertEdge(3,0,1);
        g.insertEdge(4,0,1);
        g.insertEdge(4,5,1);
        g.insertEdge(3,4,1);
        g.insertEdge(5,3,1);
#include<bits/stdc++.h>
using namespace std;
# define INF 0x3f3f3f3f

// creating a struct for an edge
struct Edge
{
    int u, v, wt;
};

class Graph
{
    int V;
    // adjacency list
    list < pair <int, int > >*adjacency;

    vector < Edge > edges;

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

    // declaring all the functions
    // inserting an edge
    void insertEdge ( int u, int v, int w );

    // deleting an edge
    void deleteEdge( int u, int v, int w );

    // finding minimum path
    int minimumPath (int u, int v );

    // deleting an edge
    void deleteEdge( int u, int v );

    // finding min cycles
    int FindMinCycle();

};

// inserting an edge
void Graph :: insertEdge ( int u, int v, int w )
{
    adjacency[u].push_back( make_pair( v, w ));
    adjacency[v].push_back( make_pair( u, w ));

    Edge e = { u, v, w };
    edges.push_back ( e );
}

// deleting an edge
void Graph :: deleteEdge( int u, int v, int w )
{
    adjacency[u].remove(make_pair( v, w ));
    adjacency[v].remove(make_pair(u, w ));
}

// finding minimum path function
int Graph :: minimumPath ( int u, int v )
{
    // storing vertices
    set< pair<int, int> > setds;

    // vector for distances
    vector<int> dist(V, INF);

    /* insert self source at first and initialize its distance as 0 */ 
    setds.insert(make_pair(0, u));
    dist[u] = 0;

    while (!setds.empty())
    {
/* The first vertex in Set is the one with the shortest distance; remove it from Set. */
        pair<int, int> tmp = *(setds.begin());
        setds.erase(setds.begin());

/* To preserve the vertices sorted distance, vertex label must be put in second of pair (distance must be first item in pair) */
        int u = tmp.second;
        list< pair<int, int> >::iterator i;
        for (i = adjacency[u].begin(); i != adjacency[u].end(); ++i)
        {
            int v = (*i).first;
            int weight = (*i).second;

            if (dist[v] > dist[u] + weight)
            {
/* If the distance of v is not INF, then it must be in our set; therefore, it should be removed and reinserted with an updated shorter distance. We only remove from Set the vertices for which the distance has been determined. Therefore, they would never see us arrive here. */
                if (dist[v] != INF)
                setds.erase(setds.find(make_pair(dist[v], v)));
                dist[v] = dist[u] + weight;
                setds.insert(make_pair(dist[v], v));
            }
        }
    }

    return dist[v] ;
}

// finding minimum path function
int Graph :: FindMinCycle ( )
{
    int min_cycle = INT_MAX;
    int E = edges.size();
    for ( int i = 0 ; i < E ; i++ )
    {
        Edge e = edges[i];
/* Obtain the edge vertices that we currently delete from the graph, and then use Dijkstra's shortest path technique to discover the shortest path between these two vertices. */
        deleteEdge( e.u, e.v, e.wt ) ;

        int dist = minimumPath( e.u, e.v );

/* If this is the shortest cycle, update min cycle; otherwise, add weight to currently deleted edges to create a cycle. */
        min_cycle = min(min_cycle, dist + e.wt);

        // add current edge back to the graph
        insertEdge( e.u, e.v, e.wt );
    }

    return min_cycle ;
}

int main()
{
    
    int V = 6;
    Graph g(V);

    g.insertEdge(0,1,1);
        g.insertEdge(2,3,1);
        g.insertEdge(1,2,1);
        g.insertEdge(3,0,1);
        g.insertEdge(4,0,1);
        g.insertEdge(4,5,1);
        g.insertEdge(3,4,1);
        g.insertEdge(5,3,1);
    cout << "Minimum weight cycle in the graph is: "<<g.FindMinCycle() << endl;
    return 0;
}
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • Where is your code that attempts to read the file? It seems like you posted a lot of code that is unrelated to the topic of the question. You may want to read the instructions for a [mcve] – drescherjm Dec 01 '22 at 17:36
  • FYI -- 1) `#include` -- Use the proper header files, not this one. 2) `list < pair >*adjacency;... adjacency = new list < pair >[V];` -- I guess you missed the point of why `std::vector` exists, even though you are using it in your code. `std::vector>> adjacency;` and then `Graph(int V) : adjacency(V) {}` -- no need for `new[]`. – PaulMcKenzie Dec 01 '22 at 17:47
  • 1
    What exactly is the problem that you are having? Is your problem reading one line at a time from the file as a string? Or is the problem dividing the strings of the individual lines into strings for the individual fields (for example `2 3 1` to the fields `2`, `3` and `1`)? Or is your problem converting a string to an `int`? Please restrict your question to one problem only. Please note that we are not a code-writing service. – Andreas Wenzel Dec 01 '22 at 18:08
  • Option #2 of this answer should help you to read a file line by line: [https://stackoverflow.com/a/7868998/487892](https://stackoverflow.com/a/7868998/487892) – drescherjm Dec 01 '22 at 18:34

1 Answers1

0

Simply use std::ifstream to open the file, and then use operator>> to read the values from it (it will handle skipping all of the whitespace in between the values), eg:

#include <iostream>
#include <fstream>
using namespace std;

...

int main()
{
    int V, u, v, w;

    ifstream ifs("filename.txt");
    if (!ifs.is_open())
    {
        cerr << "Cannot open the file" << endl;
        return 0;
    }

    ifs >> V;
    Graph g(V);

    while (ifs >> u >> v >> w) {
        g.insertEdge(u, v, w);
    }

    cout << "Minimum weight cycle in the graph is: " << g.FindMinCycle() << endl;
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770