tags:

views:

81

answers:

2

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)
  a[E++]=Edge(v,w);

      }

      return a;
}
+2  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.

Andre Holzner
A: 

There are several ways of representing a graph.

  1. Adjacency List
  2. Adjacency Matrix
  3. 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.

MAK