private static bool CanSupport(UndirectedGraph <string, Edge <string> > queryGraph, string node_H, UndirectedGraph <string, Edge <string> > inputGraph, string node_G) { // 1. Based on their degrees if (inputGraph.AdjacentDegree(node_G) < queryGraph.AdjacentDegree(node_H)) { return(false); } //So, deg(g) >= deg(h). //2. Based on the degree of their neighbors var gNeighbors = inputGraph.GetNeighbors(node_G, true); var hNeighbors = queryGraph.GetNeighbors(node_H, false); for (int i = hNeighbors.Count - 1; i >= 0; i--) { for (int j = gNeighbors.Count - 1; j >= 0; j--) { if (inputGraph.AdjacentDegree(gNeighbors[j]) >= queryGraph.AdjacentDegree(hNeighbors[i])) { gNeighbors = null; hNeighbors = null; return(true); } } } gNeighbors = null; hNeighbors = null; return(false); }
/// <summary> /// DFS is applied to traverse graph with color for bipartite /// </summary> /// <param name="n">N.</param> /// <param name="subGraph" /> /// <param name="maxHash"></param> /// <param name="colorDictionary"></param> private bool DfsSearch(UndirectedGraph <IHash, Edge <IHash> > n, UndirectedGraph <IHash, Edge <IHash> > subGraph, ref IHash maxHash, Dictionary <IHash, int> colorDictionary) { //stack Stack <IHash> stack = new Stack <IHash>(); stack.Push(maxHash); subGraph.AddVertex(maxHash); int color = 1; colorDictionary[maxHash] = color; bool res = true; while (stack.Count > 0) { IHash cur = stack.Pop(); maxHash = n.AdjacentDegree(maxHash) > n.AdjacentDegree(cur) ? maxHash : cur; //opposite color color = colorDictionary[cur] * -1; foreach (var edge in n.AdjacentEdges(cur)) { IHash nei = edge.Source == cur ? edge.Target : edge.Source; //color check if (colorDictionary.Keys.Contains(nei)) { if (colorDictionary[nei] != color) { res = false; } } else { //add vertex subGraph.AddVertex(nei); colorDictionary.Add(nei, color); stack.Push(nei); } //add edge if (!subGraph.ContainsEdge(edge)) { subGraph.AddEdge(edge); } } } return(res); }
/// <inheritdoc /> protected override void InternalCompute() { var graph = new UndirectedGraph <TVertex, TEdge>( VisitedGraph.AllowParallelEdges, VisitedGraph.EdgeEqualityComparer); graph.AddVerticesAndEdgeRange(VisitedGraph.Edges); while (!graph.IsEdgesEmpty) { TEdge[] graphEdges = graph.Edges.ToArray(); // Get a random edge int randomEdgeIndex = _rng.Next(graphEdges.Length - 1); TEdge randomEdge = graphEdges[randomEdgeIndex]; TVertex source = randomEdge.Source; TVertex target = randomEdge.Target; if (graph.AdjacentDegree(randomEdge.Source) > 1 && !_coverSet.Contains(source)) { _coverSet.Add(source); } if (graph.AdjacentDegree(randomEdge.Target) > 1 && !_coverSet.Contains(target)) { _coverSet.Add(target); } if (graph.AdjacentDegree(randomEdge.Target) == 1 && graph.AdjacentDegree(randomEdge.Source) == 1) { if (!_coverSet.Contains(source)) { _coverSet.Add(source); } graph.RemoveEdges( graph.AdjacentEdges(source).ToArray()); } else { TEdge[] edgesToRemove = graph.AdjacentEdges(target) .Concat(graph.AdjacentEdges(source)) .ToArray(); graph.RemoveEdges(edgesToRemove); } } }
private bool SatisfiesDiracTheorem(TVertex vertex) { // Using Dirac's theorem: // if |vertices| >= 3 and for any vertex deg(vertex) >= (|vertices| / 2) // then graph is Hamiltonian return(_graph.AdjacentDegree(vertex) >= _threshold); }
public static void removeVertexByDegree(List <int> N, List <int> U, List <int> V, List <double> W = null, List <Point3d> P = null, int degree = 3) { //1.0 Create undirected graph UndirectedGraph <string, TaggedEdge <string, double> > graph = GetUndirectedFullGraph(N, U, V, P); //2.0 Collect vertices that have certain degree of valence List <string> HighlightedNodes = new List <string>(); foreach (var v in graph.Vertices) { //Rhino.RhinoApp.WriteLine(graph.AdjacentDegree(v).ToString()); if (graph.AdjacentDegree(v) == degree) { Rhino.RhinoApp.WriteLine(degree.ToString()); HighlightedNodes.Add(v); } } //3.0 Remove vertices for (int i = 0; i < HighlightedNodes.Count; i++) { graph.RemoveVertex(HighlightedNodes[i]); } //4.0 Identify disconnected components var dfs = new QuickGraph.Algorithms.ConnectedComponents.ConnectedComponentsAlgorithm <string, TaggedEdge <string, double> >(graph); dfs.Compute(); Rhino.RhinoApp.WriteLine("Components count " + dfs.ComponentCount.ToString()); // graph.ConnectedComponents() }
/// <summary> /// verify graph connectivity for synchronously process /// </summary> /// <param name="n"></param> /// <param name="hashHeap"></param> /// <param name="hashToGraph"></param> private void SubGraphs(UndirectedGraph <IHash, Edge <IHash> > n, BinaryHeap <int, IHash> hashHeap, Dictionary <IHash, UndirectedGraph <IHash, Edge <IHash> > > hashToGraph) { // Bipartite Graph check Dictionary <IHash, int> colorDictionary = new Dictionary <IHash, int>(); foreach (var hash in n.Vertices) { if (colorDictionary.Keys.Contains(hash)) { continue; } UndirectedGraph <IHash, Edge <IHash> > subGraph = new UndirectedGraph <IHash, Edge <IHash> >(); // hash with most dependencies in the graph IHash maxHash = hash; bool isBipartite = DfsSearch(n, subGraph, ref maxHash, colorDictionary); if (isBipartite) { //TODO : if bipartite, parallel process for tasks in both sets asynchronously; /*foreach (var h in subGraph.Vertices) * { * if (colorDictionary[h]==1) * { * Console.Write("white:" + (char)h.GetHashBytes()[0]+" "); * } * if (colorDictionary[h]==-1) * { * Console.Write("black:" + (char)h.GetHashBytes()[0]+" "); * } * * }*/ continue; } //if not Bipartite, add maxhash to heap and hashToGraph Dictionary hashHeap.Add(subGraph.AdjacentDegree(maxHash), maxHash); hashToGraph[maxHash] = subGraph; } }
private bool SatisfiesEulerianCondition([NotNull] TVertex vertex) { return(_graph.AdjacentDegree(vertex) % 2 == 0); }
public bool satisfiesEulerianCondition(TVertex vertex) { return(graph.AdjacentDegree(vertex) % 2 == 0); }
public int GetVertexDegree(int i) { return(graph.AdjacentDegree(i)); }