public IEnumerable <T> Edges(T vertex) { if (!Verticles.ContainsKey(vertex)) { throw new ArgumentException("Vertex is not in this graph."); } return(Verticles[vertex].Edges.Select(x => x.Value)); }
public GraphVertex <T> FindVertex(T value) { if (Verticles.ContainsKey(value)) { return(Verticles[value]); } return(null); }
/// <summary> /// Returns the vertex with a given value. /// </summary> /// <param name="value"></param> /// <returns></returns> public WeightedDiGraphVertex <T, TW> FindVertex(T value) { if (Verticles.ContainsKey(value)) { return(Verticles[value]); } return(null); }
public IEnumerable <Tuple <T, TW> > InEdges(T vertex) { if (!Verticles.ContainsKey(vertex)) { throw new ArgumentException("Vertex is not in this graph."); } return(Verticles[vertex].InEdges.Select(x => new Tuple <T, TW>(x.Key.Value, x.Value))); }
/// <summary> /// Do we have an edge between the given source and destination? /// Time complexity: O/1 /// </summary> /// <param name="source"></param> /// <param name="destination"></param> /// <returns></returns> public bool HasEdge(T source, T destination) { if (!Verticles.ContainsKey(source) || !Verticles.ContainsKey(destination)) { throw new ArgumentException("Source or Destination is not in this graph."); } return(Verticles[source].Edges.Contains(Verticles[destination]) && Verticles[destination].Edges.Contains(Verticles[source])); }
/// <summary> /// Remove an existing vertex from this graph. /// Time complexity: O(V) where V is the number of verticles. /// </summary> /// <param name="vertex"></param> /// <returns></returns> public void RemoveVertex(T vertex) { if (vertex == null) { throw new ArgumentNullException(); } if (!Verticles.ContainsKey(vertex)) { throw new Exception("Vertex is not in this graph."); } foreach (var v in Verticles[vertex].Edges) { v.Edges.Remove(Verticles[vertex]); } Verticles.Remove(vertex); }
/// <summary> /// Add an edge from source to destination vertex. /// Time complexity: O/1. /// </summary> /// <param name="source"></param> /// <param name="destination"></param> public void AddEdge(T source, T destination) { if (source == null || destination == null) { throw new ArgumentException(); } if (!Verticles.ContainsKey(source) || !Verticles.ContainsKey(destination)) { throw new Exception("Source or Destination Vertex not in this graph."); } if (!Verticles[source].OutEdges.Contains(Verticles[destination]) || Verticles[destination].InEdges.Contains(Verticles[source])) { throw new Exception("Edge already exists"); } Verticles[source].OutEdges.Add(Verticles[destination]); Verticles[destination].InEdges.Add(Verticles[source]); }
/// <summary> /// Remove an edge from this graph. /// </summary> /// <param name="scource"></param> /// <param name="destination"></param> public void RemoveEdge(T source, T destination) { if (source == null | destination == null) { throw new ArgumentException(); } if (!Verticles.ContainsKey(source) || !Verticles.ContainsKey(destination)) { throw new Exception("Source or destination Vertex is not in this graph."); } if (!Verticles[source].Edges.Contains(Verticles[destination]) || !Verticles[destination].Edges.Contains(Verticles[source])) { throw new Exception("Edge does not exist"); } Verticles[source].Edges.Remove(Verticles[destination]); Verticles[destination].Edges.Remove(Verticles[source]); }
/// <summary> /// Remove the given vertex. /// Time complexity: O(V) where V is the number of verticles. /// </summary> public void RemoveVertex(T value) { if (value == null) { throw new ArgumentNullException(); } if (!Verticles.ContainsKey(value)) { throw new Exception("Vetex is not in this graph."); } foreach (var vertex in Verticles[value].InEdges) { vertex.Key.OutEdges.Remove(Verticles[value]); } foreach (var vertex in Verticles[value].OutEdges) { vertex.Key.InEdges.Remove(Verticles[value]); } Verticles.Remove(value); }
/// <summary> /// Returns the vertex object with given value /// </summary> /// <param name="value"></param> /// <returns></returns> public DiGraphVertex <T> FindVertex(T value) { return(Verticles.ContainsKey(value) ? Verticles[value] : null); }