fex
fex

Reputation: 3558

Adjacency List Graph implementation - Reusable classes

I'm storing Graph (in my Graph class) as an: Dictionary< Vertex<T>, List<Vertex<T>> _edges.

How should I store properties like weight, color etc. of edge? (to make it reusable for instance for WeightedGraphs, ColoredGraphs etc.) To create generic Graph class I thought about adding IGraph interface (which will contain common Graph operations like Traverse, InsertEdge etc.) as property in Graph class.

However I have no idea how to deal with edge properties. If I implement IGraph interface as ColoredGraph I'd like to have Vertex with color property, for IGraph implemented as WeightedGraph I'd like to have weight property and so on. I'd also like to hear how would you implement Graph as adjacency list.

ps: it's not school homework

Upvotes: 1

Views: 584

Answers (2)

gnerkus
gnerkus

Reputation: 12019

You can implement this by utilizing a Tuple<> as the vertex of the graph. The Tuple<> would allow you to store various properties of the edge.

The signature of the graph could be defined as:

Dictionary< Vertex<T>, List<Tuple<T, T>> _edges.

The first item in the Tuple indicates the destination node for the edge. The rest of the items in the Tuple would hold the properties of that edge.

Upvotes: 0

Asp Asp
Asp Asp

Reputation: 874

Edge ={2 Vertices = {Adjacent Vertices per a vertex}, weighted value, color value, etc}

THen

public Vertex
{
//get adjacent list of vertices
}
public class Edge
{
//define necesary values
//define pair of vertices to make an edge
}

Upvotes: 1

Related Questions