public void Girth() { Dictionary <int, Dictionary <int, int> > bfsPaths = new Dictionary <int, Dictionary <int, int> >(); for (int inx = 0; inx < graph.V; inx++) { var bfsPath = new BfsPaths(graph, inx); Console.WriteLine(string.Format("{0}:{1}", inx, bfsPath)); bfsPaths.Add(inx, bfsPath.edgeTo); } for (int v = 0; v < graph.V; v++) { IList <int> adjs = graph.Adj(v); int adjsCount = adjs.Count; if (adjsCount == 0 || adjsCount == 1) { continue; // in case of isolated node or node with one connection } // here we know that v has at least 2 node adjacent to it for (int inx = 0; inx < adjsCount; inx++) { List <int> adjNodeIds = new List <int>(adjs); adjNodeIds.RemoveAt(inx); FindCircle(adjs[inx], adjNodeIds, bfsPaths, v); } } }
public GraphProperties(Graph graph) { this.graph = graph; BfsPaths bfsPaths = new BfsPaths(graph, 0); for (int v = 0; v < graph.V; v++) { pathLens.Add(v, bfsPaths.HasPathTo(v) ? bfsPaths.DistTo(v) : -1); } int max = int.MinValue; int min = int.MaxValue; foreach (var kvp in pathLens) { if (kvp.Value == -1) { continue; } if (kvp.Value > max) { max = kvp.Value; maxVertexIndex = kvp.Key; } if (kvp.Value < min) { min = kvp.Value; minVertexIndex = kvp.Key; } } if (max == int.MinValue) { maxVertexIndex = -1; } if (min == int.MaxValue) { minVertexIndex = -1; } }
public static void Main(string filename, char delimitor, string source, string sink) { SymbolGraph symbolGraph = new SymbolGraph(filename, delimitor); if (!symbolGraph.Contains(source)) { Console.WriteLine(string.Format("Source {0} is out of DB", source)); } if (!symbolGraph.Contains(sink)) { Console.WriteLine(string.Format("Sink {0} is out of DB", sink)); } int sourceIndex = symbolGraph.GetIndex(source); int sinkIndex = symbolGraph.GetIndex(sink); BfsPaths bfsTree = new BfsPaths(symbolGraph.Graph, sourceIndex); IEnumerable <string> paths = bfsTree.PathTo(sinkIndex).Select(index => symbolGraph.GetName(index)); Console.WriteLine(string.Format("{0} -> {1}\n {2}", source, sink, String.Join("\n ", paths))); }