// Set a vertexe's value public Boolean SetVertexValue(Guid vertexId, Int32 newValue) { if (!this.VertexExists(vertexId)) { return(false); } UVertex uv = this.Vertices[vertexId]; uv.Value = newValue; return(true); }
// Add a vertex public Boolean AddVertex(UVertex vertexToAdd) { if (VertexExists(vertexToAdd.VertexId) == true) { return(false); } this.Vertices.Add(vertexToAdd.VertexId, vertexToAdd); this.AdjacencyMatrix.Add(vertexToAdd.VertexId, new List <Guid>()); this.IncidenceMatrix.Add(vertexToAdd.VertexId, new List <Guid>()); 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); }
//public int VertexCount => this.Vertices.Count; //public int EdgeCount => this.Edges.Count; // adjacent: whether there is an edge from vertex x to vertex y public Boolean Adjacent(Guid headVertexId, Guid tailVertexId) { if (this.Vertices.ContainsKey(headVertexId) == false) { return(false); } UVertex uv = this.Vertices[headVertexId]; foreach (Guid neighId in uv.Neighbors) { if (neighId == tailVertexId) { return(true); } } return(false); }
// neighbors: list all neighbors of a given vertex. public List <Guid> Neighbors(Guid vertexId) { UVertex uv = this.Vertices[vertexId]; return(uv.Neighbors); }