问题
I am trying to represent a Graph in Adjacency List
using C#, like the code bellow.
But I would like to know where I can find a better implementation in C#.
Like this website for Java: http://algs4.cs.princeton.edu/41undirected/Graph.java.html
In order to improve this implementation I have some question:
- Is there another simple data structure to use, and that you able to make operations like
DFS
,BFS
,Find the Shortest-path
easier ? Or the data structure vary too much according to the problem to be solved ?
=== EDITED ===
I've tried to implement the data structure as bellow.
OBS: This approach seems simple, but I realize later that is not very suitable for a DFS, for example, since you need to keep track of the first element of the LinkedList
all the time.In my solution Seems that is better to use a custom created Linked List, instead of
LinkedList<Vertex>
.
Considering the comments bellow and to maintain simplicity I made some changes. But I don't know if that changes would affect further operations, like a BFS
.
To be able to have direct and indirect Graph I think is better to use an interface than a property.
public interface IGraph
{
void InsertEdge(int edgeAKey, int edgeBKey);
void IsertNewVertex(int vertexKey);
LinkedList<Vertex> FindByKey(int vertexKey);
bool ExistKey(int vertexKey);
}
To make it as simple as possible we can use already implemented data structures like Dictionary
and LinkedList
. And instead of using an object
as Dictionary key
, to make it simple we can create in Vertex
a key
(or label
) and a value
, if you would like to add a value that already exist in another Vertex
.
public class GraphDirect : IGraph
{
private Dictionary<int,LinkedList<Vertex>> Vertexes { get; set; }
public GraphDirect()
{
Vertexes = new Dictionary<int, LinkedList<Vertex>>();
}
public bool ExistKey(int vertexKey)
{
if (this.FindByKey(vertexKey) == null)
return false;
else
return true;
}
public void IsertNewVertex(int vertexKey)
{
if (!this.ExistKey(vertexKey))
{
Vertex vertex = new Vertex(vertexKey);
LinkedList<Vertex> listVertexes = new LinkedList<Vertex>();
listVertexes.AddFirst(vertex);
this.Vertexes.Add(vertexKey, listVertexes);
}
}
public void InsertEdge(int vertexAKey, int vertexBKey)
{
//Create the vertex A, if it doesn't exist
if (!this.ExistKey(vertexAKey))
{
this.IsertNewVertex(vertexAKey);
}
//Will always insert the vertex B on this edge
this.FindByKey(vertexAKey).AddLast(new Vertex(vertexBKey));
//Create the vertex B, if doesn't exist
if (!this.ExistKey(vertexBKey))
{
this.IsertNewVertex(vertexBKey);
}
}
public LinkedList<Vertex> FindByKey(int vertexKey)
{
if (this.Vertexes.ContainsKey(vertexKey))
return this.Vertexes[vertexKey];
return null;
}
}
The Vertex class doesn't need any other pointer, just keep the key
and a value
, if necessary.
public enum State { Visited = 0, UnVisited = 1, Processed = 2 }
public class Vertex
{
public int Key;
public int Value;
public State Status = State.UnVisited;
public Vertex(int key)
{
this.Key = key;
this.Value = 0;
}
public Vertex(int key, int value)
{
this.Key = key;
this.Value = value;
}
}
public class Start
{
public static void Main(){
GraphDirect gDirect = new GraphDirect();
gDirect.InsertEdge(2, 3);
gDirect.InsertEdge(2, 8);
gDirect.InsertEdge(5, 6);
}
}
来源:https://stackoverflow.com/questions/13819744/graph-adjacency-list-implementation