public List <WordPair> GenerateAllNaivePairs(BidirectionalGraph <Word, Edge <Word> > graph) { /*BidirectionalGraph<Word, Edge<Word>> graph = new BidirectionalGraph<Word, Edge<Word>>(false); #region Prepare graph * foreach (var item in g.Vertices) * graph.AddVertex(item); #endregion*/ List <WordPair> pairs = new List <WordPair>(); //List<KeyValuePair<Word, Word>> pairs = new List<KeyValuePair<Word, Word>>(); //List<KeyValuePair<List<KeyValuePair<Word, Word>>, float>> pairsProb = new List<KeyValuePair<List<KeyValuePair<Word, Word>>, float>>(); var uWords = graph.Vertices.Where(t => t.Language == Language.Uyghur); var kWords = graph.Vertices.Where(t => t.Language == Language.Kazak); var cWords = graph.Vertices.Where(t => t.Language == Language.Chinese); foreach (var uWord in uWords) { float connectedUC = (float)graph.InDegree(uWord); foreach (var kWord in kWords) { float connectedCK = (float)graph.InDegree(kWord); WordPair pair = new WordPair(uWord, kWord); pair.Prob = (connectedUC + connectedCK) / (2 * cWords.Count()); pairs.Add(pair); } } /*var output = pairs.Select(t => string.Format("{0},{1}", t.Key, t.Value)); * System.IO.File.WriteAllLines(@"buffer\NaiveCombination.txt", output); * System.Media.SoundPlayer simpleSound = new System.Media.SoundPlayer(@"c:\Windows\Media\Ring03.wav"); * simpleSound.Play(); * Debug.WriteLine("Generate All Naive pairs is done"); */ return(pairs); }
public void isRoot(BidirectionalGraph <string, Edge <string> > graph, Edge <string> e) { if (graph.InDegree(e.Source) == 0) { Roots.Add(e.Source); } }
void UpdateVectorsT() { if (this.CGraph.VertexCount == 0) { return; } BidirectionalGraph <Node, Edge> topoGraph = this.CGraph.Clone(); while (topoGraph.VertexCount > 0) { Node current = topoGraph.Vertices.Where(item => topoGraph.InDegree(item) == 0).First(); IEnumerable <Edge> outEdges = topoGraph.Edges.Where(item => item.Source.Equals(current)); foreach (Edge outEdge in outEdges) { Node succ = outEdge.Target; Node successor = this.CGraph.Vertices.Where(v => v.Equals(succ)).Single(); for (int i = 0; i < this.VcCount; i++) { //succ.VectorClock[i] = Math.Max(succ.VectorClock[i], current.VectorClock[i]); successor.VectorClock[i] = Math.Max(successor.VectorClock[i], current.VectorClock[i]); } } topoGraph.RemoveVertex(current); } }
public void BalanceUnBalancedFlowGraph() { g = GraphFactory.UnBalancedFlow(); IVertex source = null; IVertex sink = null; foreach (IVertex v in g.Vertices) { if (g.InDegree(v) == 0) { source = v; continue; } if (g.OutDegree(v) == 0) { sink = v; continue; } } Assert.IsNotNull(source); Assert.IsNotNull(sink); int vertexCount = g.VerticesCount; int edgeCount = g.EdgesCount; algo = new GraphBalancerAlgorithm(g, source, sink); algo.DeficientVertexAdded += new VertexEventHandler(algo_DeficientVertexAdded); algo.SurplusVertexAdded += new VertexEventHandler(algo_SurplusVertexAdded); algo.Balance(); VerifyBalanced(vertexCount, edgeCount); }
public void isRoot(string e) { // Debug.Log(e); // Debug.Log(graph.InDegree(e)); if (graph.InDegree(e) == 0) { Roots.Add(e); } }
public static IEnumerable <string> Get_First_Mutual_Branch_Point_Backwards(string vertex, BidirectionalGraph <string, TaggedEdge <string, Tag> > graph) { Contract.Requires(graph != null); if (!graph.ContainsVertex(vertex)) { yield break; } int inDegree = graph.InDegree(vertex); if (inDegree < 2) { yield break; // the provided vertex is not a mergePoint } if (inDegree == 2) { string s1 = graph.InEdge(vertex, 0).Source; string s2 = graph.InEdge(vertex, 1).Source; if (s1 != s2) { HashSet <string> branchPoints1 = new HashSet <string>(); HashSet <string> branchPoints2 = new HashSet <string>(); foreach (string v in Get_First_Branch_Point_Backwards(s1, graph)) { branchPoints1.Add(v); } foreach (string v in Get_First_Branch_Point_Backwards(s2, graph)) { branchPoints2.Add(v); } foreach (string mutual in branchPoints1.Intersect(branchPoints2)) { yield return(mutual); } } } else { Console.WriteLine("WARNING: Get_First_Mutual_Branch_Point_Backwards: multiple merge points at this the provided vertex " + vertex); } }
public static IBidirectionalGraph <TVertex, TEdge> CreateDAG <TVertex, TEdge>( int vertexCount, int edgeCount, int maxParent, int maxChild, bool parallelEdgeAllowed, [NotNull, InstantHandle] Func <int, TVertex> vertexFactory, [NotNull, InstantHandle] Func <TVertex, TVertex, TEdge> edgeFactory, [NotNull] Random random) where TEdge : IEdge <TVertex> { var dagGraph = new BidirectionalGraph <TVertex, TEdge>(parallelEdgeAllowed, vertexCount); var verticesMap = new Dictionary <int, TVertex>(); for (int i = 0; i < vertexCount; ++i) { TVertex vertex = vertexFactory(i); verticesMap[i] = vertex; dagGraph.AddVertex(vertex); } for (int i = 0; i < edgeCount; ++i) { TVertex parent; TVertex child; do { int childIndex = random.Next(vertexCount - 1) + 1; int parentIndex = random.Next(childIndex); child = verticesMap[childIndex]; parent = verticesMap[parentIndex]; } while (!parallelEdgeAllowed && dagGraph.ContainsEdge(parent, child) || dagGraph.OutDegree(parent) >= maxChild || dagGraph.InDegree(child) >= maxParent); // Create the edge between the 2 vertex dagGraph.AddEdge(edgeFactory(parent, child)); } return(dagGraph); }
public static IBidirectionalGraph <TVertex, TEdge> CreateDAG <TVertex, TEdge>(int vertexCount, int edgeCount, int maxParent, int maxChild, bool parallelEdgeAllowed, Func <int, TVertex> vertexFactory, Func <TVertex, TVertex, TEdge> edgeFactory) where TEdge : IEdge <TVertex> { BidirectionalGraph <TVertex, TEdge> dagGraph = new BidirectionalGraph <TVertex, TEdge>(false, vertexCount); Dictionary <int, TVertex> vertexMap = new Dictionary <int, TVertex>(); for (int i = 0; i < vertexCount; i++) { TVertex v = vertexFactory(i); vertexMap[i] = v; dagGraph.AddVertex(v); } Random rnd = new Random(DateTime.Now.Millisecond); int childIndex; int parentIndex; TVertex child; TVertex parent; for (int i = 0; i < edgeCount; i++) { do { childIndex = rnd.Next(vertexCount - 1) + 1; parentIndex = rnd.Next(childIndex); child = vertexMap[childIndex]; parent = vertexMap[parentIndex]; }while ((!parallelEdgeAllowed && dagGraph.ContainsEdge(parent, child)) || dagGraph.OutDegree(parent) >= maxChild || dagGraph.InDegree(child) >= maxParent); //create the edge between the 2 vertex TEdge e = edgeFactory(parent, child); dagGraph.AddEdge(e); } return(dagGraph); }
/// <inheritdoc /> protected override void InternalCompute() { if (VisitedGraph.VertexCount == 0) { return; } if (Parameters.Direction == LayoutDirection.LeftToRight || Parameters.Direction == LayoutDirection.RightToLeft) { // Change the sizes foreach (KeyValuePair <TVertex, Size> sizePair in _verticesSizes.ToArray()) { _verticesSizes[sizePair.Key] = new Size(sizePair.Value.Height, sizePair.Value.Width); } } _direction = Parameters.Direction == LayoutDirection.RightToLeft || Parameters.Direction == LayoutDirection.BottomToTop ? -1 : 1; GenerateSpanningTree(); // First layout the vertices with 0 in-edge foreach (TVertex source in _spanningTree.Vertices.Where(v => _spanningTree.InDegree(v) == 0)) { CalculatePosition(source, null, 0); } // Then the others foreach (TVertex source in _spanningTree.Vertices) { CalculatePosition(source, null, 0); } AssignPositions(); }
public override void Compute(CancellationToken cancellationToken) { if (Parameters.Direction == LayoutDirection.LeftToRight || Parameters.Direction == LayoutDirection.RightToLeft) { //change the sizes foreach (var sizePair in _sizes.ToArray()) { _sizes[sizePair.Key] = new Size(sizePair.Value.Height, sizePair.Value.Width); } } if (Parameters.Direction == LayoutDirection.RightToLeft || Parameters.Direction == LayoutDirection.BottomToTop) { _direction = -1; } else { _direction = 1; } GenerateSpanningTree(cancellationToken); //DoWidthAndHeightOptimization(); //first layout the vertices with 0 in-edge foreach (var source in _spanningTree.Vertices.Where(v => _spanningTree.InDegree(v) == 0)) { CalculatePosition(source, null, 0); } //then the others foreach (var source in _spanningTree.Vertices) { CalculatePosition(source, null, 0); } AssignPositions(cancellationToken); }
private bool TryRemoveDummyEdge(InternalActivityEdge edge) { bool edgeRemoved = false; // If this is a single edge out or a single edge in - it adds no information to the graph and can be merged. var outDegree = arrowGraph.OutDegree(edge.Source); var inDegree = arrowGraph.InDegree(edge.Target); // Remove the vertex which has no other edges connected to it if (outDegree == 1) { IEnumerable <InternalActivityEdge> allIncoming; if (!arrowGraph.TryGetInEdges(edge.Source, out allIncoming)) { allIncoming = new List <InternalActivityEdge>(); } bool abortMerge = WillParallelEdgesBeCreated(allIncoming, null, edge.Target); if (!abortMerge) { // Add the edges with the new source vertex // And remove the old edges foreach (var incomingEdge in allIncoming.ToList()) { arrowGraph.AddEdge(new InternalActivityEdge(incomingEdge.Source, edge.Target, incomingEdge.IsCritical, incomingEdge.ActivityId)); arrowGraph.RemoveEdge(incomingEdge); } // Remove the edge which is no longer needed arrowGraph.RemoveEdge(edge); // Now remove the vertex which is no longer needed arrowGraph.RemoveVertex(edge.Source); edgeRemoved = true; } } else if (inDegree == 1) { IEnumerable <InternalActivityEdge> allOutgoing; if (!arrowGraph.TryGetOutEdges(edge.Target, out allOutgoing)) { allOutgoing = new List <InternalActivityEdge>(); } bool abortMerge = WillParallelEdgesBeCreated(allOutgoing, edge.Source, null); if (!abortMerge) { // Add the edges with the new source vertex // And remove the old edges foreach (var outgoingEdge in allOutgoing.ToList()) { arrowGraph.AddEdge(new InternalActivityEdge(edge.Source, outgoingEdge.Target, outgoingEdge.IsCritical, outgoingEdge.ActivityId)); arrowGraph.RemoveEdge(outgoingEdge); } // Remove the edge which is no longer needed arrowGraph.RemoveEdge(edge); // Now remove the vertex which is no longer needed arrowGraph.RemoveVertex(edge.Target); edgeRemoved = true; } } return(edgeRemoved); }
private WordPair createNewEdges(Word uWord, Word kWord, BidirectionalGraph <Word, Edge <Word> > semiCompleteGraph, BidirectionalGraph <Word, Edge <Word> > completeGraph)//, int uCount, int kCount) { Cache1.Clear(); List <SPath> paths = new List <SPath>(); WordPair pair = new WordPair(uWord, kWord); pair.Polysemy = 0f; foreach (var item in semiCompleteGraph.OutEdges(uWord)) // Using the updated graph with new edges { int inDegreePivot = (int)semiCompleteGraph.InDegree(item.Target); int outDegreePivot = (int)semiCompleteGraph.OutDegree(item.Target); int totalSenseEdge = (Math.Max(inDegreePivot, outDegreePivot) - 1) * (inDegreePivot + outDegreePivot); pair.Polysemy += totalSenseEdge; //Word pivot_sense = item.Target; //for (int sense = 1; sense <= totalSense; sense++) //{ //pivot_sense.Value = pivot_sense.Value + "_sense" + sense; SLink linkCU = new SLink(item.Target, uWord); linkCU.Exists = true;// semiCompleteGraph.ContainsEdge(uWord, pivot_sense); SLink linkCK = new SLink(item.Target, kWord); linkCK.Exists = semiCompleteGraph.ContainsEdge(item.Target, kWord); SPath path = new SPath(linkCU, linkCK); paths.Add(path); Cache1.Add(item.Target.ID, true); //} } foreach (var item in semiCompleteGraph.InEdges(kWord)) // Using the updated graph with new edges { if (Cache1.ContainsKey(item.Source.ID)) { continue; } int inDegreePivot = (int)semiCompleteGraph.InDegree(item.Source); int outDegreePivot = (int)semiCompleteGraph.OutDegree(item.Source); int totalSenseEdge = (Math.Max(inDegreePivot, outDegreePivot) - 1) * (inDegreePivot + outDegreePivot); pair.Polysemy += totalSenseEdge; SLink linkCK = new SLink(item.Source, kWord); linkCK.Exists = true;// semiCompleteGraph.ContainsEdge(item.Source, kWord); SLink linkCU = new SLink(item.Source, uWord); linkCU.Exists = semiCompleteGraph.ContainsEdge(uWord, item.Source); SPath path = new SPath(linkCU, linkCK); paths.Add(path); } //calculate probability //float couverage = Math.Min(uCount, kCount) / (float)Math.Max(uCount, kCount); float pUK = 0; float pKU = 0; float probUK = 0; float probKU = 0; //bool hasPolysemy = false; foreach (var item in paths) { //if (!item.LinkCU.Exists || !item.LinkCK.Exists) //containning non-existance link if (!item.LinkCU.Exists) { if (languageOption == 2) { completeGraph.AddEdge(new Edge <Word>(item.LinkCU.WordNonPivot, item.LinkCU.WordPivot)); } continue; } if (!item.LinkCK.Exists) { if (languageOption == 2) { completeGraph.AddEdge(new Edge <Word>(item.LinkCK.WordPivot, item.LinkCK.WordNonPivot)); } continue; } //if ((float)graph.InDegree(item.LinkCU.WordPivot) > 1 || (float)graph.OutDegree(item.LinkCK.WordPivot) > 1) // hasPolysemy = true; if (currentCycle == 1) { float PrCU = 1.0f / (float)semiCompleteGraph.OutDegree(item.LinkCU.WordNonPivot); //P(C|U) = P(C&U)/P(U) float PrKC = 1.0f / (float)semiCompleteGraph.OutDegree(item.LinkCK.WordPivot); //P(K|C) = P(K&C)/P(C) float PrCK = 1.0f / (float)semiCompleteGraph.InDegree(item.LinkCK.WordNonPivot); //P(C|K) = P(C&K)/P(K) float PrUC = 1.0f / (float)semiCompleteGraph.InDegree(item.LinkCU.WordPivot); //P(U|C) = P(U&C)/P(C) pKU += PrCU * PrKC; pUK += PrCK * PrUC; } else { float PrCU = 0.0f; float PrKC = 0.0f; float PrCK = 0.0f; float PrUC = 0.0f; foreach (var downEdgeCU in semiCompleteGraph.OutEdges(item.LinkCU.WordNonPivot)) //Loop through down-path from nonpivot1 to pivot { PrCU += 1.0f / LinkWeightCache[new SLink(downEdgeCU.Target, downEdgeCU.Source)]; //P(C|U) = P(C&U)/P(U) } foreach (var downEdgeCK in semiCompleteGraph.OutEdges(item.LinkCK.WordPivot)) //Loop through down-path from pivot to nonpivot2 { PrKC += 1.0f / LinkWeightCache[new SLink(downEdgeCK.Source, downEdgeCK.Target)]; //P(K|C) = P(K&C)/P(C) } foreach (var upEdgeCK in semiCompleteGraph.InEdges(item.LinkCK.WordNonPivot)) //Loop through up-path from nonpivot2 to pivot { PrCK += 1.0f / LinkWeightCache[new SLink(upEdgeCK.Target, upEdgeCK.Source)]; //P(C|K) = P(C&K)/P(K) } foreach (var upEdgeCU in semiCompleteGraph.InEdges(item.LinkCU.WordPivot)) //Loop through up-path from pivot to nonpivot1 { PrUC += 1.0f / LinkWeightCache[new SLink(upEdgeCU.Source, upEdgeCU.Target)]; //P(U|C) = P(U&C)/P(C) } PrCU = 1.0f / PrCU; PrKC = 1.0f / PrKC; PrCK = 1.0f / PrCK; PrUC = 1.0f / PrUC; pUK += PrUC * PrCK; pKU += PrKC * PrCU; } } probUK = pUK * pKU; //WordPair pair = new WordPair(uWord, kWord); //pair.HasMissingEdge = hasPolysemy; pair.Paths = paths; pair.Prob = (float)probUK; pair.Polysemy *= (1 - pair.Prob); //set link weights foreach (var item in pair.Paths) { //CU //float polysemyCost = 1 / ((float)graph.InDegree(item.LinkCU.WordPivot) * (float)graph.OutDegree(item.LinkCK.WordPivot)); if (item.LinkCU.Exists) { item.LinkCU.Pr = 1f; //polysemyCost; if (!LinkWeightCache.ContainsKey(item.LinkCU)) { LinkWeightCache.Add(item.LinkCU, item.LinkCU.Pr); } } else { //pair.HasMissingCUEdge = true; float value = 0; if (LinkWeightCache.TryGetValue(item.LinkCU, out value)) { if (pair.Prob > value) { item.LinkCU.Pr = LinkWeightCache[item.LinkCU] = pair.Prob; //polysemyCost * } else { item.LinkCU.Pr = value; } } else { item.LinkCU.Pr = pair.Prob; //polysemyCost * LinkWeightCache.Add(item.LinkCU, pair.Prob); } } //CK if (item.LinkCK.Exists) //false)// { item.LinkCK.Pr = 1f; //polysemyCost if (!LinkWeightCache.ContainsKey(item.LinkCK)) { LinkWeightCache.Add(item.LinkCK, item.LinkCK.Pr); } } else { float value = 0; if (LinkWeightCache.TryGetValue(item.LinkCK, out value)) { if (pair.Prob > value) { LinkWeightCache[item.LinkCK] = item.LinkCK.Pr = pair.Prob; //polysemyCost * } else { item.LinkCK.Pr = value; } } else { item.LinkCK.Pr = pair.Prob; //polysemyCost * LinkWeightCache.Add(item.LinkCK, pair.Prob); } } } return(pair); }
/// <summary> /// Returns the number of vertices which connect to the current vertex. /// </summary> /// <param name="origin">The vertex for which the number of inbound connections should be returned.</param> /// <returns>The number of inbound connections for the current vertex.</returns> public int NumberOfInboundConnections(IScheduleVertex origin) { return(m_Graph.InDegree(origin)); }
public int InDegree(TVertex v) => _graph.InDegree(v);