简体   繁体   中英

C/C++: Creating simple graph library

I've been thinking about creating a class in C++ on graph theory. The idea is it'll be a class to hold indefinite number of vertices and edges for a simple graph (at most one edge between a pair of vertices). The problem is how'd I store this indefinite number of vertices/edges in the most efficient way.

I came up with the idea of having dynamic pointer to array of vertices as a member in the class. However, it'd be inefficient, and I also encounter problem of how to determine the connection of vertices (I wouldn't be able to determine which vertices connect with which), if I use this method. The alternative is to create a class Vertex that suppose to contain information of its connectivity. However, because of indefinite number of edges, I cannot think of other way around other than to use dynamic variables inside Vertex. It'd make my code efficiency worse with this approach.

So is there a better approach?

If you do not plan to frequently add and remove items from inside the collections, I'd use STL vectors . They're fast for iterating through, but not terrible for inserts and removes in the middle.

If you want to add / remove anywhere frequently, I'd use STL lists . They're slower for iterating, but insertion / removal is O(1).

You can then define your vertex and edge as something like:

class Edge;

class Vertex
{
    // ...
public:
    std::list<Edge> incomingEdges;
    std::list<Edge> outgoingEdges;
}

class Edge
{
    // ...
public:
    Vertex startpoint;
    Vertex endpoint;
}

You'll pretty quickly find yourself wanting both a Vertex and Edge class -- there are too many algorithms that depend on coloring, or weighting, or marking edges, and it's also simpler to mix directed and undirected edges. The odds are good that you aren't going to really care a lot about storing the appropriate references dynamically, because that can be reduced to a vector of pointers.

Another issue to think about is if you will want to store this thing persistently.

Suggestion: try the Simplest Thing That Can Possibly Work first. Assuming an Array class that resizes itself as needed, that will look something like

class Vertex {
  Array<Edge> edges ;
  VertexData vd ; // define this for the task.

  public:
     // ctor etc; quiz: what operations?
}

class Edge {
  Vertex v1, v2;
  EdgeData ed;
  public:
     // ctor etc
}

Construct all the vertices and edges with new , don't worry about performance,and write some code against these classes.

Then go back, think how you'd have liked to write the code, and re-implement the classes to have that interface.

I'm a little prejudiced, since I used to teach the book and worked for Marshall Cline and Mike Girou, but I think one of the best C++ books for someone trying to really use it effectively is The C++ FAQBook , by Cline, Girou, and Lomow.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM