implementation GRaph ADT in c++ [closed]
i am trying to implement Graph ADT in c++ here is code
#include <iostream>
using namespace std;
struct Edge{
int v,w;
Edge( int t=-1,int k=-1):v(t),w(k){}
};
class Graph {
public:
Graph(int,bool);
~Graph();
int V() const;
int E() const;
bool directed() const;
int remove(Edge);
int insert(Edge);
bool edge(int,int);
class AdjIterator{
public:
AdjIterator(const Graph&,int);
int beg();
int nxt();
bool end();
};
};
int main(){
return 0;
}
how good is this kind of implementation according to performance of code? Edited: i have added this code
template<class Graph>
vector<Edge> edge(Graph& G){
int E=0;
vector<Edge>a(G.E());
for (int v=0;v<G.V();v++){
typename Graph::AdjIterator A(G,v);
for (int w=A.beg();w!=A.end();w=A.nxt())
if (G.directed() || v<w开发者_JAVA百科)
a[E++]=Edge(v,w);
}
return a;
}
There is not much of implementation in the code you show, just the interface.
The other major way of representing graphs is through an adjacency matrix.
Which way of representing the graph is better depends on your application.
There are several ways of representing a graph.
- Adjacency List
- Adjacency Matrix
- Edge List (which is what your implementation seems to be).
The choice of implementation really depends on what your going to be using it for. Adjacency list is quite fast and compact for sparse graphs and is often preferred for most algorithms. Adjacency matrix is generally easier to implement and some algorithms (like Flowd-Warshall all pairs shortest path algorithm) require it. Edge lists are useful when all you need are the edges, e.g. in an implementation that uses Kruskal's Algorithm.
It is hard to say whether this particular implementation is suited for your purpose without know what it is that you will be using it for.
精彩评论