public void AddVertex(GraphVertex vertex) { if (!Contains(vertex)) { vertices.Add(vertex); } }
/// <summary> /// Adds an edge between 2 vertices of given cost /// </summary> /// <param name="one">A vertex</param> /// <param name="two">A second vertex</param> /// <param name="cost">Weight of edge between the two</param> public void AddEdge(GraphVertex one, GraphVertex two, double cost, bool calculateCost) { if (Contains(one) & Contains(two)) { if (!one.Neighbors.Contains(two)) { if (!calculateCost) { one.Neighbors.Add(two); one.Weights.Add(cost); two.Neighbors.Add(one); two.Weights.Add(cost); } if (calculateCost) { one.Neighbors.Add(two); one.Weights.Add(Distance(one.Coordinates, two.Coordinates)); two.Neighbors.Add(one); two.Weights.Add(Distance(one.Coordinates, two.Coordinates)); } } } }
public void RemoveVertex(GraphVertex vert) { foreach (GraphVertex v in vertices) { if (v == vert) { vertices.Remove(v); } } }
public bool IsNeighbor(GraphVertex ver) { if (neighbors.Contains(ver)) { return(true); } else { return(false); } }
public bool Contains(GraphVertex ver1) { bool contain = false; foreach (GraphVertex vert in vertices) { if (vert == ver1) { contain = true; } } return(contain); }
public double GetEdgeWeight(GraphVertex v1, GraphVertex v2) { int locationV1 = vertices.IndexOf(v1); int locationV2 = vertices[locationV1].Neighbors.IndexOf(v2); if (locationV1 != -1 & locationV2 != -1) { return(vertices[locationV1].Weights[locationV2]); } else { throw new ArgumentOutOfRangeException(); } }
public static Graph CompleteGraphConstructor(int vertexTotal, double cost) { Graph result = new Graph("Complete_" + vertexTotal); GraphVertex[] vertexArray = new GraphVertex[vertexTotal]; for (int i = 0; i < vertexTotal; i++) { GraphVertex toAdd = new GraphVertex(i.ToString(), 0, 0); result.AddVertex(toAdd); vertexArray[i] = toAdd; } result.AddSameWeightedEdges(cost, vertexArray); return(result); }
private Graph ConnectComponents(List <GraphVertex> vertices) { Graph result = new Graph("connected"); result.AddVertices(vertices.ToArray()); for (int i = 0; i < result.Vertices.Count - 1; i++) { GraphVertex first = result.Vertices[i]; GraphVertex second = result.Vertices[i + 1]; // result.AddEdge(first,second) } return(Graph.CompleteGraphConstructor(2, 3)); }