Exemplo n.º 1
0
 private static void RelaxNeighbores(Verticle v)
 {
     foreach (Edge e in v.EdgesOut)
     {
         Relax(e);
     }
 }
Exemplo n.º 2
0
        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));
        }
Exemplo n.º 3
0
        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);
                    }
                }
            }
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
 public Edge(Verticle v, Verticle u, int w = 1)
 {
     Contract.Requires(v != null);
     Contract.Requires(u != null);
     From   = v;
     To     = u;
     Weight = w;
 }
Exemplo n.º 6
0
		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);
				}
		}
Exemplo n.º 7
0
        public void Dfs(string sId)
        {
            Contract.Requires(sId != null);
            Contract.Requires(ContainsVerticle(sId));
            ResetStats();
            Verticle s = GetVerticle(sId);

            s.Dist = 0;
            Dfs(s);
        }
Exemplo n.º 8
0
        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;
            }
        }
Exemplo n.º 9
0
 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);
 }
Exemplo n.º 10
0
        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);
        }
Exemplo n.º 11
0
		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;
		}
Exemplo n.º 12
0
		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;
		}
Exemplo n.º 13
0
        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);
                }
            }
        }
Exemplo n.º 14
0
        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);
            }
        }
Exemplo n.º 15
0
        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);
        }
Exemplo n.º 16
0
        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);
        }
Exemplo n.º 17
0
 public bool AddVerticle(Verticle v) =>
 AddVerticle(v?.Id);
Exemplo n.º 18
0
 public bool ContainsVerticle(Verticle v) =>
 ContainsVerticle(v?.Id);