I have a text file that contains an adjacency list of undirected graph, example:
1 2 3
2 1 3 4
3 1 2 4
4 2 3
I am reading the file and storing it in an adjacency list,
I want to transfer this data to an Edge List,
I am trying to find out all Unique pair of edges,
Using the DFS I am getting the pairs but those are not unique,
I am trying to eliminate the duplicate pairs ( a duplicate pair is <1,2> is same as <2,1>) but it is not happening.......
Here is the outut I am getting:
degree of 1 is : 2
Nember of vertices is :4
Numbe rof edges is : 5
Max degree is : 3
Min degree is : 2
List of edges : ..........
1 2
Edge 1 2 Inserted
Back : 2 1
Edge 2 1 Inserted
2 3
Edge 2 3 Inserted
Back : 3 1
Edge 3 1 Inserted
Back : 3 2
Edge 3 2 Inserted
3 4
Edge 3 4 Inserted
Back : 4 2
Edge 4 2 Inserted
Back : 4 3
Edge 4 3 Inserted
Back : 2 4
Edge 2 4 Inserted
Back : 1 3
Edge 1 3 Inserted
Thanks... Sorry for the long code.........
#include <iostream>
#include <fstream>
#include <limits>
#include <vector>
#include <list>
#include <string>
#include <sstream>
#include <cstdlib>
#include <map>
using namespace std;
typedef struct EdgeList
{
int u;
int v;
}EdgeList;
class Graph
{
public:
vector<list<int> > adj;
int V;
int E;
EdgeList *edges;
Graph(int N):adj(N+1)
{
V = N;
}
Graph(int N, int M):adj(N+1)
{
V = N;
E = M;
int x = E+550;
edges = new EdgeList[x];
}
void addEdgeList(Graph g, int u, int v);
bool ischeckEdge(Graph g,int u,int v);
void addEdge(int u, int v);
void deleteSelfLoops(Graph g);
int degree(Graph g, int v);
int maxDegree(Graph g);
int minDegree(Graph g);
int edgeCount(Graph g);
int numberOfSelfLoops(Graph g);
bool isEdge(Graph g, int u,int v);
void DFS(Graph g);
void dfsUtil(Graph g, int v, bool visited[] );
};
int e = 0;
void Graph::addEdgeList(Graph g, int u, int v)
{
if(ischeckEdge(g,u,v))
{
cout<<"Edge "<<u<<" "<<v<<" Inserted "<<endl;
g.edges[e].u = u;
g.edges[e].v = v;
e++;
}
}
bool Graph::ischeckEdge(Graph g, int u, int v)
{
bool a,b,c,d;
for(int i=0;i<=g.E; i++)
{
if( ((g.edges[i].u == u || g.edges[i].v == v) && (!g.edges[i].u == u || !g.edges[i].v == v)) )
{
cout<<"First block" ;
return false;
}
else if( ((g.edges[i].u == v || g.edges[i].v == u) && (!g.edges[i].u == v || !g.edges[i].v == u)))
{
cout<<" sec block";
return false;
}
else
{
return true;
}
}
}
void Graph::dfsUtil(Graph g, int v, bool visited[])
{
list<int> temp;
temp = g.adj[v];
if(!visited[v])
{
visited[v] = true;
}
list<int>::iterator j;
for(j = temp.begin();j != temp.end(); j++)
{
if(!visited[*j])
{
cout<<endl<<v<<" "<<*j<<endl;// print edges
g.addEdgeList(g,v,*j);
dfsUtil(g, *j, visited);
}
else //back edge
{
cout<<" Back : "<<v<<" "<<*j<<endl;
g.addEdgeList(g,v,*j);
}
}
}
void Graph::DFS(Graph g)
{
bool *visited = new bool[g.V+1];
int *parent = new int[g.V+1];
for(int i = 0; i <= g.V; i++)
{
visited[i] = false;
parent[i] = -1;
}
for(int v = 0; v <= g.V; v++)
{
if(!visited[v])
{
dfsUtil(g, v , visited);
}
}
}
bool Graph::isEdge(Graph g, int u, int v)
{
for(int w : adj[v])
{
if(u == w)
{
return true;
}
}
return false;
}
void Graph::addEdge(int u, int v)
{
adj[u].push_back(v); //undirected graph
//adj[v].push_back(u);
}
void Graph::deleteSelfLoops(Graph g)
{
for(int v = 0; v <= g.V; v++)
{
for(int w : g.adj[v])
{
if(v == w)
{
g,adj[v].remove(w);
}
}
}
}
int Graph::degree(Graph g, int v)
{
int deg = 0;
for(int i = 0;i < g.adj[v].size();i++)
{
deg++;
}
return deg;
}
int Graph::edgeCount(Graph g)
{
int edge = 0;
int i;
if(g.adj[0].empty())
{
i = 1;
}
else
{
i = 0;
}
for(i; i <= g.V; i++)
{
edge = edge + degree(g,i);
}
return edge/2; //handshaking lemma
}
int Graph::maxDegree(Graph g)
{
int max = 0;
for(int i = 0; i <= g.V; i++)
{
if(degree(g,i) > max)
{
max = degree(g,i);
}
}
return max;
}
int Graph::minDegree(Graph g)
{
int min = g.V;
int i;
if(g.adj[0].empty())
{
i = 1;
}
else
{
i = 0;
}
for(i; i <= g.V; i++)
{
if(degree(g,i) < min)
{
min = degree(g,i);
}
}
return min;
}
int Graph::numberOfSelfLoops(Graph g)
{
int count = 0;
for(int v = 0; v <= g.V; v++)
{
for(int w : g.adj[v])
{
if(v == w)
{
count++;
}
}
}
return count / 2;
}
int main()
{
ifstream fin("AdjList.txt");
int V = 0;
string str;
while(getline(fin, str))
{
istringstream ss(str);
int num;
while(ss >> num)
{
// ... you now get a number ...
int u;
u = str[0] - '0'; //char can be coverted to int
if(num != u )
{
//cout<<u<< " "<< num<<endl;
}
}
V++;
}
fin.close();
Graph g1(V);
fin.open("AdjList.txt");
while(getline(fin, str))
{
istringstream ss(str);
int num;
while(ss >> num)
{
// ... you now get a number ...
int u;
u = str[0] - '0'; //char can be coverted to int
if(num != u )
{
g1.addEdge(u,num);
//cout<<u<< " "<< num<<endl;
}
}
}
fin.close();
Graph g(V,g1.edgeCount(g1));
g.adj = g1.adj;
cout<<"degree of 1 is : "<<g.degree(g,1)<<endl;
cout<<"Nember of vertices is :"<<g.V<<endl;
cout<<"Numbe rof edges is : "<< g.edgeCount(g)<<endl;
cout<<"Max degree is : "<<g.maxDegree(g)<<endl;
cout<<"Min degree is : "<<g.minDegree(g)<<endl;
cout<<"List of edges : .........."<<endl;
cout<< g.edges[4].u;
for(int i=0; i<=g.E+500;i++)
{
g.edges[i].u=0;
g.edges[i].v =0;
}
g.DFS(g);
return 0;
}
Since you index vertices with integers, the simple trick you can do is to add edge {v, u}
if and only if v <= u
(if there are no self-loops then v < u
is sufficient). In order to do that, just change:
if(!visited[*j])
{
cout<<endl<<v<<" "<<*j<<endl;// print edges
g.addEdgeList(g,v,*j);
dfsUtil(g, *j, visited);
}
to
if(!visited[*j])
{
cout<<endl<<v<<" "<<*j<<endl;// print edges
if(v <= *j)
{
g.addEdgeList(g,v,*j);
}
dfsUtil(g, *j, visited);
}
as I was thinking the problem was in ischeckEdge. it was comparing (0,0) as edges as in the input there is no vertex '0',
bool Graph::ischeckEdge(Graph g, int u, int v)
{
bool a = false;
swap(u,v);
for(int i=0;i<=e; i++)
{
if(i == 0)
{
continue;
}
if(i ==1 && g.edges[i].u ==0 && g.edges[i].v ==0)
{
return true;
}
if(g.edges[i].u == u && g.edges[i].v ==v )
{
//cout<<"Same Edge"<<endl;
a= false;
break;
}
else
{
//cout<<"Edge count : "<<e<<endl;
//cout<<"Iteration : "<<i<<endl;
//cout<<"edge u v : "<<g.edges[i].u<<" "<<g.edges[i].v<<endl;
//cout<<"input u v : "<<u<<" "<<v<<endl;
a= true;
}
}
return a;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.