// Add an edge public Boolean AddEdge(UEdge edgeToAdd) { if (this.Edges.ContainsKey(edgeToAdd.EdgeId)) { return(false); } this.Edges.Add(edgeToAdd.EdgeId, edgeToAdd); this.AdjacencyMatrix[edgeToAdd.HeadVertexId].Add(edgeToAdd.TailVertexId); this.AdjacencyMatrix[edgeToAdd.TailVertexId].Add(edgeToAdd.HeadVertexId); this.IncidenceMatrix[edgeToAdd.HeadVertexId].Add(edgeToAdd.EdgeId); this.IncidenceMatrix[edgeToAdd.TailVertexId].Add(edgeToAdd.EdgeId); return(true); }
// Remove an edge public Boolean RemoveEdge(Guid edgeToRemove) { UEdge ue = this.Edges[edgeToRemove]; Guid headVertexId = ue.HeadVertexId; Guid tailVertexId = ue.TailVertexId; if (!this.Edges.ContainsKey(edgeToRemove)) { return(false); } this.Edges.Remove(edgeToRemove); this.AdjacencyMatrix[headVertexId].Remove(tailVertexId); this.AdjacencyMatrix[tailVertexId].Remove(headVertexId); this.IncidenceMatrix[headVertexId].Remove(edgeToRemove); this.IncidenceMatrix[tailVertexId].Remove(edgeToRemove); return(true); }
//private static readonly GraphUtils instance = new GraphUtils(); /// <summary> /// /// </summary> //private GraphUtils() //{ //} /// <summary> /// /// </summary> //public static GraphUtils Instance //{ // get // { // return instance; // } //} /// <summary> /// /// </summary> /// <param name="appContext"></param> /// <returns></returns> public static UGraph GenerateRandomGraph(AppContext appContext) { Int32 numNodes = appContext.RandomSource.Next(Defines.MIN_NUM_NODES, Defines.MAX_NUM_NODES); //appContext.CurrentGraph = new UGraph(); UGraph outGraph = new UGraph(); //appContext.CurrentGraphState = GraphState.New; for (Int32 i = 0; i < numNodes; i++) { UVertex uv = new UVertex { Value = appContext.RandomSource.Next() }; outGraph.AddVertex(uv); } foreach (KeyValuePair <Guid, UVertex> kvp1 in outGraph.Vertices) { foreach (KeyValuePair <Guid, UVertex> kvp2 in outGraph.Vertices) { if (kvp1.Value.VertexId != kvp2.Value.VertexId) { Int32 prob = appContext.RandomSource.Next(1, 10); if (prob >= Defines.EDGE_PROBABILITY * 10) { UEdge ue = new UEdge { HeadVertexId = kvp1.Value.VertexId, TailVertexId = kvp2.Value.VertexId, Value = appContext.RandomSource.Next() }; outGraph.Edges.Add(ue.EdgeId, ue); kvp1.Value.AddNeighbor(kvp2.Value.VertexId); kvp1.Value.AddEdge(ue.EdgeId); kvp2.Value.AddNeighbor(kvp1.Value.VertexId); kvp2.Value.AddEdge(ue.EdgeId); } } } } return(outGraph); }