private static void RelaxNeighbores(Verticle v) { foreach (Edge e in v.EdgesOut) { Relax(e); } }
private Graph Prim() { if (!IsConnected()) { throw new InvalidOperationException("Graph is not connected"); } ResetStats(); Verticle v = verticles.First().Value; v.Visited = true; Edge[] treeEdges = new Edge[NumOfVerticles - 1]; int teIndex = 0; var edges = new Heap <Edge>(v.EdgesOut); Edge e; while (teIndex < numOfVerticles - 1) { e = GetMinUnvisitedEdge(edges); treeEdges[teIndex++] = e; v = e.To; v.Visited = true; edges.AddRange(v.EdgesOut); } return(GetSubGraph(treeEdges)); }
public void Bfs(string sId) { Contract.Requires(sId != null); ResetStats(); var queue = new Queue <Verticle>(); Verticle v = GetVerticle(sId); v.Dist = 0; v.Visited = true; queue.Enqueue(v); Verticle u; while (queue.Count > 0) { v = queue.Dequeue(); foreach (Edge e in v.EdgesOut) { u = e.To; if (!u.Visited) { u.Visited = true; u.Dist = v.Dist + 1; u.ParentEdge = e; queue.Enqueue(u); } } } }
public bool AddEdge(string vId, string uId, int w = 1) { Contract.Requires(vId != null); Contract.Requires(uId != null); Contract.Ensures(ContainsEdge(vId, uId)); if (ContainsEdge(vId, uId)) { return(false); } AddVerticle(vId); AddVerticle(uId); Verticle v = GetVerticle(vId); Verticle u = GetVerticle(uId); Edge e = new Edge(v, u, w); v.AddOutNeighbor(e); u.AddInNeighbor(e); if (!IsDirected) { e = new Edge(u, v, w); v.AddInNeighbor(e); u.AddOutNeighbor(e); } return(true); }
public Edge(Verticle v, Verticle u, int w = 1) { Contract.Requires(v != null); Contract.Requires(u != null); From = v; To = u; Weight = w; }
private void CleanForward(FlowNetwork Lf,Verticle v) { if(v.NumOfEdgesIn==0) foreach(Edge e in v.EdgesOut.ToList()){ Lf.G.RemoveEdge(e); CleanForward(Lf,e.To); } }
public void Dfs(string sId) { Contract.Requires(sId != null); Contract.Requires(ContainsVerticle(sId)); ResetStats(); Verticle s = GetVerticle(sId); s.Dist = 0; Dfs(s); }
private static void Relax(Edge e) { Verticle v = e.From; Verticle u = e.To; if (u.Dist > v.Dist + e.Weight) { u.Dist = v.Dist + e.Weight; u.ParentEdge = e; } }
public bool AddVerticle(string vId) { Contract.Requires(vId != null); Contract.Ensures(ContainsVerticle(vId)); if (ContainsVerticle(vId)) { return(false); } numOfVerticles++; verticles[vId] = new Verticle(vId); return(true); }
private Verticle FindMinUnvisitedVerticle() { Verticle res = null; foreach (Verticle v in verticles.Values) { if (!v.Visited && (res == null || res.Dist > v.Dist)) { res = v; } } return(res); }
private static List<Edge> FindBackwardPath(Graph G,string uId,string vId) { Verticle r = G.GetVerticle(vId); Verticle u = G.GetVerticle(uId); var path = new List<Edge>(); Edge e; while(r!=u) { e = r.EdgesIn.First(); path.Add(e); r = e.From; } path.Reverse(); return path; }
private Func<Edge,int> FindBlockingFlow(FlowNetwork Lf) { Func<Edge,int> b = (e=>0); Verticle t = Lf.G.GetVerticle(tId); List<Edge> path; Func<Edge,int> g; while(t.NumOfEdgesIn>0) { path = FindBackwardPath(Lf.G,sId,tId); g = FindPathFlow(path,Lf.c); b = AddFlow(b,g); CleanSaturatedEdges(Lf,b,path); Lf.G.Bfs(sId); } return b; }
private void Dfs(Verticle v) { v.Visited = true; Verticle u; foreach (Edge e in v.EdgesOut) { u = e.To; if (!u.Visited) { u.ParentEdge = e; u.Dist = v.Dist + 1; Dfs(u); } } }
public void Dijkstra(string sId) // Complexity: O(V^2) { Contract.Requires(sId != null); Contract.Requires(ContainsVerticle(sId)); ResetStats(); Verticle v = verticles[sId]; v.Visited = true; v.Dist = 0; RelaxNeighbores(v); for (int i = 1; i < numOfVerticles; i++) { v = FindMinUnvisitedVerticle(); v.Visited = true; RelaxNeighbores(v); } }
public bool RemoveEdge(string vId, string uId) { Contract.Requires(vId != null); Contract.Requires(uId != null); Contract.Ensures(!ContainsEdge(vId, uId)); if (!ContainsEdge(vId, uId)) { return(false); } Verticle v = GetVerticle(vId); Verticle u = GetVerticle(uId); v.RemoveEdgeOut(uId); u.RemoveEdgeIn(vId); if (!IsDirected) { v.RemoveEdgeIn(uId); u.RemoveEdgeOut(vId); } return(true); }
public List <Edge> GetShortestPath(string sId, string tId, bool statsReady = false) { Contract.Requires(sId != null); Contract.Requires(ContainsVerticle(sId)); Contract.Requires(tId != null); Contract.Requires(ContainsVerticle(tId)); if (!statsReady) { Bfs(sId); } Verticle s = GetVerticle(sId); Verticle v = GetVerticle(tId); List <Edge> path = new List <Edge>(); while (v != s) { path.Add(v.ParentEdge); v = v.ParentEdge.From; } path.Reverse(); return(path); }
public bool AddVerticle(Verticle v) => AddVerticle(v?.Id);
public bool ContainsVerticle(Verticle v) => ContainsVerticle(v?.Id);