/// <summary> /// Determines whether a graph has an Eulerian cycle using necessary /// and sufficient conditions (without computing the cycle itself): /// - at least one EdgeW /// - degree(v) is even for every vertex v /// - the graph is connected (ignoring isolated vertices) /// </summary> /// <param name="g"></param> /// <returns></returns> private static bool HasEulerianCycle(Graph g) { // Condition 0: at least 1 EdgeW if (g.E == 0) { return(false); } // Condition 1: degree(v) is even for every vertex for (var v = 0; v < g.V; v++) { if (g.Degree(v) % 2 != 0) { return(false); } } // Condition 2: graph is connected, ignoring isolated vertices var s = NonIsolatedVertex(g); var bfs = new BreadthFirstPaths(g, s); for (var v = 0; v < g.V; v++) { if (g.Degree(v) > 0 && !bfs.HasPathTo(v)) { return(false); } } return(true); }
public void Find(string sink) { var s = _sg.Index(_source); var bfs = new BreadthFirstPaths(_g, s); if (_sg.Contains(sink)) { var t = _sg.Index(sink); if (bfs.HasPathTo(t)) { foreach (int v in bfs.PathTo(t)) { Console.WriteLine($" {_sg.Name(v)}"); } } else { Console.WriteLine("Not connected"); } } else { Console.WriteLine(" Not in database."); } }
/// <summary> /// Determines whether a digraph has an Eulerian path using necessary /// and sufficient conditions (without computing the path itself): /// - indegree(v) = outdegree(v) for every vertex, /// except one vertex v may have outdegree(v) = indegree(v) + 1 /// (and one vertex v may have indegree(v) = outdegree(v) + 1) /// - the graph is connected, when viewed as an undirected graph /// (ignoring isolated vertices) /// This method is solely for unit testing. /// </summary> /// <param name="g"></param> /// <returns></returns> private static bool HasEulerianPath(Digraph g) { if (g.E == 0) { return(true); } // Condition 1: indegree(v) == outdegree(v) for every vertex, // except one vertex may have outdegree(v) = indegree(v) + 1 var deficit = 0; for (var v = 0; v < g.V; v++) { if (g.Outdegree(v) > g.Indegree(v)) { deficit += (g.Outdegree(v) - g.Indegree(v)); } } if (deficit > 1) { return(false); } // Condition 2: graph is connected, ignoring isolated vertices var h = new Graph(g.V); for (var v = 0; v < g.V; v++) { foreach (int w in g.Adj(v)) { h.AddEdge(v, w); } } // check that all non-isolated vertices are connected var s = NonIsolatedVertex(g); var bfs = new BreadthFirstPaths(h, s); for (var v = 0; v < g.V; v++) { if (h.Degree(v) > 0 && !bfs.HasPathTo(v)) { return(false); } } return(true); }
/// <summary> /// Determines whether a digraph has an Eulerian cycle using necessary /// and sufficient conditions (without computing the cycle itself): /// - at least one edge /// - indegree(v) = outdegree(v) for every vertex /// - the graph is connected, when viewed as an undirected graph /// (ignoring isolated vertices) /// </summary> /// <param name="g"></param> /// <returns></returns> private static bool HasEulerianCycle(Digraph g) { // Condition 0: at least 1 edge if (g.E == 0) { return(false); } // Condition 1: indegree(v) == outdegree(v) for every vertex for (var v = 0; v < g.V; v++) { if (g.Outdegree(v) != g.Indegree(v)) { return(false); } } // Condition 2: graph is connected, ignoring isolated vertices var h = new Graph(g.V); for (var v = 0; v < g.V; v++) { foreach (int w in g.Adj(v)) { h.AddEdge(v, w); } } // check that all non-isolated vertices are conneted var s = NonIsolatedVertex(g); var bfs = new BreadthFirstPaths(h, s); for (var v = 0; v < g.V; v++) { if (h.Degree(v) > 0 && !bfs.HasPathTo(v)) { return(false); } } return(true); }
/// <summary> /// Determines whether a graph has an Eulerian path using necessary /// and sufficient conditions (without computing the path itself): /// - degree(v) is even for every vertex, except for possibly two /// - the graph is connected (ignoring isolated vertices) /// This method is solely for unit testing. /// </summary> /// <param name="g"></param> /// <returns></returns> private static bool HasEulerianPath(Graph g) { if (g.E == 0) { return(true); } // Condition 1: degree(v) is even except for possibly two var oddDegreeVertices = 0; for (var v = 0; v < g.V; v++) { if (g.Degree(v) % 2 != 0) { oddDegreeVertices++; } } if (oddDegreeVertices > 2) { return(false); } // Condition 2: graph is connected, ignoring isolated vertices var s = NonIsolatedVertex(g); var bfs = new BreadthFirstPaths(g, s); for (var v = 0; v < g.V; v++) { if (g.Degree(v) > 0 && !bfs.HasPathTo(v)) { return(false); } } return(true); }
/// <summary> /// Determines whether a digraph has an Eulerian path using necessary /// and sufficient conditions (without computing the path itself): /// - indegree(v) = outdegree(v) for every vertex, /// except one vertex v may have outdegree(v) = indegree(v) + 1 /// (and one vertex v may have indegree(v) = outdegree(v) + 1) /// - the graph is connected, when viewed as an undirected graph /// (ignoring isolated vertices) /// This method is solely for unit testing. /// </summary> /// <param name="g"></param> /// <returns></returns> private static bool HasEulerianPath(Digraph g) { if (g.E == 0) return true; // Condition 1: indegree(v) == outdegree(v) for every vertex, // except one vertex may have outdegree(v) = indegree(v) + 1 var deficit = 0; for (var v = 0; v < g.V; v++) if (g.Outdegree(v) > g.Indegree(v)) deficit += (g.Outdegree(v) - g.Indegree(v)); if (deficit > 1) return false; // Condition 2: graph is connected, ignoring isolated vertices var h = new Graph(g.V); for (var v = 0; v < g.V; v++) foreach (int w in g.Adj(v)) h.AddEdge(v, w); // check that all non-isolated vertices are connected var s = NonIsolatedVertex(g); var bfs = new BreadthFirstPaths(h, s); for (var v = 0; v < g.V; v++) if (h.Degree(v) > 0 && !bfs.HasPathTo(v)) return false; return true; }
/// <summary> /// Determines whether a graph has an Eulerian path using necessary /// and sufficient conditions (without computing the path itself): /// - degree(v) is even for every vertex, except for possibly two /// - the graph is connected (ignoring isolated vertices) /// This method is solely for unit testing. /// </summary> /// <param name="g"></param> /// <returns></returns> private static bool HasEulerianPath(Graph g) { if (g.E == 0) return true; // Condition 1: degree(v) is even except for possibly two var oddDegreeVertices = 0; for (var v = 0; v < g.V; v++) if (g.Degree(v) % 2 != 0) oddDegreeVertices++; if (oddDegreeVertices > 2) return false; // Condition 2: graph is connected, ignoring isolated vertices var s = NonIsolatedVertex(g); var bfs = new BreadthFirstPaths(g, s); for (var v = 0; v < g.V; v++) if (g.Degree(v) > 0 && !bfs.HasPathTo(v)) return false; return true; }
/// <summary> /// Determines whether a graph has an Eulerian cycle using necessary /// and sufficient conditions (without computing the cycle itself): /// - at least one EdgeW /// - degree(v) is even for every vertex v /// - the graph is connected (ignoring isolated vertices) /// </summary> /// <param name="g"></param> /// <returns></returns> private static bool HasEulerianCycle(Graph g) { // Condition 0: at least 1 EdgeW if (g.E == 0) return false; // Condition 1: degree(v) is even for every vertex for (var v = 0; v < g.V; v++) if (g.Degree(v) % 2 != 0) return false; // Condition 2: graph is connected, ignoring isolated vertices var s = NonIsolatedVertex(g); var bfs = new BreadthFirstPaths(g, s); for (var v = 0; v < g.V; v++) if (g.Degree(v) > 0 && !bfs.HasPathTo(v)) return false; return true; }
public void Run() { Console.WriteLine("Choose file:"); // Prompt Console.WriteLine("1 - tinyCG.txt"); // Prompt Console.WriteLine("2 - mediumG.txt"); // Prompt Console.WriteLine("3 - largeG.zip"); // Prompt Console.WriteLine("or quit"); // Prompt var fileNumber = Console.ReadLine(); var fieName = string.Empty; switch (fileNumber) { case "1": fieName = "tinyCG.txt"; break; case "2": fieName = "mediumG.txt"; break; case "3": fieName = "largeG.zip"; break; case "quit": return; default: return; } var @in = new In($"Files\\Graphs\\{fieName}"); var lines = !fieName.EndsWith("zip") ? @in.ReadAllLines() : @in.ReadAllLinesFromZip(); var lineIterator = 0; var v = 0; var e = 0; var edges = new List<EdgeU>(); foreach (var line in lines) { if (lineIterator == 0) { v = Convert.ToInt32(line); } if (lineIterator == 1) { e = Convert.ToInt32(line); } if (lineIterator > 1) { var lineSplitted = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var ve = Convert.ToInt32(lineSplitted[0]); var we = Convert.ToInt32(lineSplitted[1]); var edge = new EdgeU(ve, we); edges.Add(edge); } lineIterator++; } var graph = new Graph(v, e, edges); if (fileNumber != "3") { Console.WriteLine(graph); } const int s = 0; var bfs1 = new BreadthFirstPaths(graph, s); for (var vi = 0; vi < graph.V; vi++) { if (bfs1.HasPathTo(vi)) { Console.Write($"{s} to {vi}: "); foreach (int x in bfs1.PathTo(vi)) { if (x == s) Console.Write(x); else Console.Write($"-{x}"); } Console.WriteLine(); } else { Console.WriteLine($"{s} to {v}: not connected{Environment.NewLine}"); } if (vi >= 1 && fileNumber == "3") { break; } } //Console.WriteLine("------------------------------------------------"); Console.ReadLine(); }