/// <summary> /// (DIRECTED GRAPH) -Create a subgraph of an input graph between a start and end vertex (includes start and end vertex) /// </summary> /// <param name="startindex"></param> /// <param name="endvertex"></param> /// <param name="graph"></param> /// <returns></returns> public IDigraph CreateSubGraph(int startindex, int endvertex, IDigraph graph) { IDigraph subgraph = new Digraph(graph.VertexCount); for (int i = 0; i < graph.VertexCount; i++) { subgraph.AddVertex(); } for (int i = startindex; i <= endvertex; i++) { List <int> neighbors = (List <int>)graph.GetVertexNeighborsOut(i); foreach (int neighbor in neighbors) { if (neighbor >= startindex && neighbor <= endvertex) { subgraph.AddEdge(i, neighbor); } } } return(subgraph); }
/// <summary> /// (DIRECTED GRAPH) -Create a copy of the directed graph that is reversed /// </summary> /// <param name="graph"></param> /// <returns></returns> public IDigraph ReverseGraph(IDigraph graph) { IDigraph reversegraph = new Digraph(graph.VertexCount); for (int i = 0; i < graph.VertexCount; i++) { reversegraph.AddVertex(); } for (int i = 0; i < graph.VertexCount; i++) { List <int> vertexneighbors = (List <int>)graph.GetVertexNeighborsOut(i); //for each vertex that is connected, create a reverse edge with its neighbors if (vertexneighbors.Count > 0) { foreach (int neighbor in vertexneighbors) { reversegraph.AddEdge(neighbor, i); } } } return(reversegraph); }