I am implementing fundamental data structures in C# while trying to learn techniques in the language to make my code cleaner, more concise, and reusable. I have implemented a generic graph with a few elementary search algorithms. What are some ways to improve my implementation and coding style?
/// <summary>
/// Implementation of a generic vertex to be used in any graph
/// </summary>
class Vertex<T>
{
List<Vertex<T>> neighbors;
T value;
bool isVisited;
public List<Vertex<T>> Neighbors { get { return neighbors; } set { neighbors = value; } }
public T Value { get { return value; } set { this.value = value; } }
public bool IsVisited { get { return isVisited; } set { isVisited = value; } }
public int NeighborsCount { get { return neighbors.Count; } }
public Vertex(T value)
{
this.value = value;
isVisited = false;
neighbors = new List<Vertex<T>>();
}
public Vertex(T value, List<Vertex<T>> neighbors)
{
this.value = value;
isVisited = false;
this.neighbors = neighbors;
}
public void Visit()
{
isVisited = true;
}
public void AddEdge(Vertex<T> vertex)
{
neighbors.Add(vertex);
}
public void AddEdges(List<Vertex<T>> newNeighbors)
{
neighbors.AddRange(newNeighbors);
}
public void RemoveEdge(Vertex<T> vertex)
{
neighbors.Remove(vertex);
}
public override string ToString()
{
StringBuilder allNeighbors = new StringBuilder("");
allNeighbors.Append(value + ": ");
foreach (Vertex<T> neighbor in neighbors)
{
allNeighbors.Append(neighbor.value + " ");
}
return allNeighbors.ToString();
}
}
--
/// <summary>
/// Implementation of a generic, unweighted, undirected graph
/// </summary>
class UndirectedGenericGraph<T>
{
// The list of vertices in the graph
private List<Vertex<T>> vertices;
// The number of vertices
int size;
public List<Vertex<T>> Vertices { get { return vertices; } }
public int Size { get { return vertices.Count; } }
public UndirectedGenericGraph(int initialSize)
{
if(size < 0)
{
throw new ArgumentException("Number of vertices cannot be negative");
}
size = initialSize;
vertices = new List<Vertex<T>>(initialSize);
}
public UndirectedGenericGraph(List<Vertex<T>> initialNodes)
{
vertices = initialNodes;
size = vertices.Count;
}
public void AddVertex(Vertex<T> vertex)
{
vertices.Add(vertex);
}
public void RemoveVertex(Vertex<T> vertex)
{
vertices.Remove(vertex);
}
public bool HasVertex(Vertex<T> vertex)
{
return vertices.Contains(vertex);
}
public void DepthFirstSearch(Vertex<T> root)
{
if (!root.IsVisited)
{
Console.Write(root.Value + " ");
root.Visit();
foreach(Vertex<T> neighbor in root.Neighbors)
{
DepthFirstSearch(neighbor);
}
}
}
public void BreadthFirstSearch(Vertex<T> root)
{
Queue<Vertex<T>> queue = new Queue<Vertex<T>>();
root.Visit();
queue.Enqueue(root);
while(queue.Count > 0)
{
Vertex<T> current = queue.Dequeue();
foreach (Vertex<T> neighbor in current.Neighbors)
{
if (!neighbor.IsVisited)
{
Console.Write(neighbor.Value + " ");
neighbor.Visit();
queue.Enqueue(neighbor);
}
}
}
}
}
--
class Program
{
static void Main(string[] args)
{
// Create a list of vertices using the Vertex<T> class
List<Vertex<string>> vertices = new List<Vertex<string>>
(
new Vertex<string> []
{
new Vertex<string>("Los Angeles"),
new Vertex<string>("San Francisco"),
new Vertex<string>("Las Vegas"),
new Vertex<string>("Seattle"),
new Vertex<string>("Austin"),
new Vertex<string>("Portland")
}
);
// Establish edges; Ex. Los Angeles -> San Francisco, Las Vegas, Portland
vertices[0].AddEdges(new List<Vertex<string>>(new Vertex<string>[]
{
vertices[1], vertices[2], vertices[5]
}));
vertices[1].AddEdges(new List<Vertex<string>>(new Vertex<string>[]
{
vertices[0], vertices[3], vertices[5]
}));
vertices[2].AddEdges(new List<Vertex<string>>(new Vertex<string>[]
{
vertices[0], vertices[1], vertices[4]
}));
vertices[3].AddEdges(new List<Vertex<string>>(new Vertex<string>[]
{
vertices[1], vertices[5]
}));
vertices[4].AddEdges(new List<Vertex<string>>(new Vertex<string>[]
{
vertices[2]
}));
vertices[5].AddEdges(new List<Vertex<string>>(new Vertex<string>[]
{
vertices[1], vertices[3]
}));
// Create graph using the UndirectedGenericGraph<T> class
UndirectedGenericGraph<string> testGraph = new UndirectedGenericGraph<string>(vertices);
// Check to see that all neighbors are properly set up
foreach(Vertex<string> vertex in vertices)
{
Console.WriteLine(vertex.ToString());
}
// Test searching algorithms
testGraph.DepthFirstSearch(vertices[0]);
//testGraph.BreadthFirstSearch(vertices[0]);
}
}
testGraph.BreadthFirstSearch(vertices[0]);
commented out? \$\endgroup\$