internal void Calculate() { IntBinaryHeapPriorityQueue pq=new IntBinaryHeapPriorityQueue(graph.NumberOfVertices); foreach(int s in source) pq.insert(s,0); while(!pq.isEmpty()) { int u=pq.del_min(); foreach(Edge l in graph.EdgesAtVertex(u)) { int v=l.target; int len=l.weight; int c=dist[u]+len; if(pred[v]==null && !sourceSet.Contains(v)) pq.insert(v,c); else if (c<dist[v]) pq.decrease_priority(v,c); else continue; dist[v]=c; pred[v]=l; } } }
/// <summary> /// Performs the main calculation. /// </summary> internal void Calculate() { IntBinaryHeapPriorityQueue pq = new IntBinaryHeapPriorityQueue(graph.NumberOfVertices); pq.insert(source, 0); while (!pq.isEmpty()) { int u = pq.del_min(); foreach (Edge l in graph.EdgesAtVertex(u)) { int v = l.target; int len = l.weight; int c = dist[u] + len; if (pred[v] == null && v != source) { pq.insert(v, c); } else if (c < dist[v]) { pq.decrease_priority(v, c); } else { continue; } dist[v] = c; pred[v] = l; } } }
void calc() { if (calcIsCalled) { return; } calcIsCalled = true; int upperBound = 0; //the init is not needed but the compiler produces an error bool upperBoundIsSet = false; IntBinaryHeapPriorityQueue pq1 = new IntBinaryHeapPriorityQueue(graph.NumberOfVertices); pq1.insert(source, 0); while (!pq1.isEmpty()) { int u = pq1.del_min(); if (freeVertices[u]) { foundFreeVertex = u; break; } foreach (Edge l in graph.EdgesAtVertex(u)) { int v = l.target; int len = l.weight; int c = dist[u] + len; if (upperBoundIsSet && c >= upperBound) { continue; } if (freeVertices[v]) { upperBound = c; upperBoundIsSet = true; } if (pred[v] == null && v != source) { pq1.insert(v, c); } else if (c < dist[v]) { pq1.decrease_priority(v, c); } else { continue; } dist[v] = c; pred[v] = l; } } }
/// <summary> /// Returns a path /// </summary> /// <returns></returns> internal Edge[] GetPath() { if(thePath!=null)return thePath; IntBinaryHeapPriorityQueue pq=new IntBinaryHeapPriorityQueue(graph.NumberOfVertices); pq.insert(source,0); while(!pq.isEmpty()) { int u=pq.del_min(); if(u==target) break; foreach(Edge l in graph.EdgesAtVertex(u)) { int v=l.target; int len=l.weight; int c=dist[u]+len; if(c > udist)continue; if(pred[v]==null && v !=source) pq.insert(v,c); else if (c<dist[v]) pq.decrease_priority(v,c); else continue; dist[v]=c; pred[v]=l; } } if(dist[target]==Graph.INF) throw new InvalidOperationException("target is not reached"); { int count=0; int v=target; while(v!=source) { v=pred[v].source; count++; } thePath=new Edge[count]; v=target; while(v!=source) { thePath[--count]=pred[v]; v=pred[v].source; } } return thePath; }
void calcDistancesFromVertex(int indexInNeg, bool[] posC, int [,] dist, ref int maxdist) { int node = neg[indexInNeg]; int [] ld = new int[n]; //local distance array IntBinaryHeapPriorityQueue pq1 = new IntBinaryHeapPriorityQueue(n); int posLeft = pos.Length; pq1.insert(node, 0); for (int i = 0; i < n; i++) { ld[i] = Graph.INF; } ld[node] = 0; while (!pq1.isEmpty()) { int u = pq1.del_min(); if (posC[u]) { if (--posLeft == 0) { break; } } foreach (Edge l in this.linksByVertex[u]) { int v = l.target; int len = l.weight; int c = ld[u] + len; if (ld[v] == Graph.INF) { pq1.insert(v, c); } else if (c < ld[v]) { pq1.decrease_priority(v, c); } else { continue; } ld[v] = c; } } if (posLeft > 0) { throw new InvalidOperationException("graph is not strongly connected"); } //put the results in dist and update maxdist for (int i = 0; i < pos.Length; i++) { int dis = dist[indexInNeg, i] = ld[pos[i]]; if (dis > maxdist) { maxdist = dis; } } }
void calcDistancesFromVertex(int indexInNeg, bool[] posC, int [,]dist, ref int maxdist) { int node=neg[indexInNeg]; int [] ld=new int[n];//local distance array IntBinaryHeapPriorityQueue pq1=new IntBinaryHeapPriorityQueue(n); int posLeft=pos.Length; pq1.insert(node,0); for(int i=0;i<n;i++) ld[i]=Graph.INF; ld[node]=0; while( ! pq1.isEmpty()) { int u=pq1.del_min(); if(posC[u]) { if (--posLeft==0) break; } foreach(Edge l in this.linksByVertex[u]) { int v=l.target; int len=l.weight; int c=ld[u]+len; if(ld[v]==Graph.INF) pq1.insert(v,c); else if (c<ld[v]) pq1.decrease_priority(v,c); else continue; ld[v]=c; } } if (posLeft>0) throw new InvalidOperationException("graph is not strongly connected"); //put the results in dist and update maxdist for(int i=0;i<pos.Length;i++) { int dis=dist[indexInNeg,i]=ld[pos[i]]; if (dis>maxdist) maxdist=dis; } }
void calc() { if(calcIsCalled) return; calcIsCalled=true; int upperBound=0; //the init is not needed but the compiler produces an error bool upperBoundIsSet=false; IntBinaryHeapPriorityQueue pq1=new IntBinaryHeapPriorityQueue(graph.NumberOfVertices); pq1.insert(source,0); while( ! pq1.isEmpty()) { int u=pq1.del_min(); if(freeVertices[u]) { foundFreeVertex=u; break; } foreach(Edge l in graph.EdgesAtVertex(u)) { int v=l.target; int len=l.weight; int c=dist[u]+len; if(upperBoundIsSet && c >= upperBound) continue; if(freeVertices[v]){upperBound=c;upperBoundIsSet=true;} if(pred[v]==null && v !=source) pq1.insert(v,c); else if (c<dist[v]) pq1.decrease_priority(v,c); else continue; dist[v]=c; pred[v]=l; } } }
/// <summary> /// Returns a path /// </summary> /// <returns></returns> internal Edge[] GetPath() { if (thePath != null) { return(thePath); } IntBinaryHeapPriorityQueue pq = new IntBinaryHeapPriorityQueue(graph.NumberOfVertices); pq.insert(source, 0); while (!pq.isEmpty()) { int u = pq.del_min(); if (u == target) { break; } foreach (Edge l in graph.EdgesAtVertex(u)) { int v = l.target; int len = l.weight; int c = dist[u] + len; if (c > udist) { continue; } if (pred[v] == null && v != source) { pq.insert(v, c); } else if (c < dist[v]) { pq.decrease_priority(v, c); } else { continue; } dist[v] = c; pred[v] = l; } } if (dist[target] == Graph.INF) { throw new InvalidOperationException("target is not reached"); } { int count = 0; int v = target; while (v != source) { v = pred[v].source; count++; } thePath = new Edge[count]; v = target; while (v != source) { thePath[--count] = pred[v]; v = pred[v].source; } } return(thePath); }