public void ShortestP(Vert sourseV)
        {
            sourseV.setDist(0);
            Queue <Vert> queue = new Queue <Vert>();

            queue.Enqueue(sourseV);
            sourseV.setVisited(true);

            while (queue.Count != 0)
            {
                Vert actualVertex = queue.Dequeue();
                foreach (Edge edge in actualVertex.getList())
                {
                    Vert v = edge.getTargetVert();
                    if (!v.Visited())
                    {
                        double newDistance = actualVertex.getDist()
                                             + edge.getWeight();
                        if (newDistance < v.getDist())
                        {
                            Queue <Vert> myQueue = new Queue <Vert>(queue.Where(x => x != v));
                            queue = myQueue;
                            v.setDist(newDistance);
                            v.setPr(actualVertex);
                            queue.Enqueue(v);
                        }
                    }
                }
                actualVertex.setVisited(true);
            }
        }
Beispiel #2
0
        // vẽ đồ thị FPG
        // 1 vẽ đỉnh
        public List <Vert> createVertex(List <TRule> rules)
        {
            List <AttributeValue> verts = new List <AttributeValue>();

            foreach (var ruleItem in rules)
            {
                foreach (var item in ruleItem.Left)
                {
                    if (!verts.Contains(item))
                    {
                        verts.Add(item);
                    }
                }
                if (!verts.Contains(ruleItem.Right))
                {
                    verts.Add(ruleItem.Right);
                }
            }
            List <Vert> vertsList = new List <Vert>();

            foreach (var item in verts)
            {
                Vert vert = new Vert(item);
                vertsList.Add(vert);
            }
            return(vertsList);
        }
        public List <Vert> getShortestP(Vert targetVertex)
        {
            List <Vert> list = new List <Vert>();

            for (Vert vertex = targetVertex; vertex != null; vertex = vertex.getPr())
            {
                list.Add(vertex);
            }
            list.Reverse();
            return(list);
        }
Beispiel #4
0
        //2 vẽ cạnh và tính hr
        public double executeHeuristic(List <TRule> rules, AttributeValue nameBeginVert, AttributeValue nameTargetVert)
        {
            // nối đỉnh thành cạnh
            List <Vert> vertsList = new List <Vert>();

            vertsList = createVertex(rules);
            foreach (TRule ruleItem in rules)
            {
                Vert vertC = new Vert(ruleItem.Right);
                int  vt    = vertsList.FindIndex(r => r.name.Equals(vertC.name));
                foreach (AttributeValue item in ruleItem.Left)
                {
                    Vert vert = new Vert(item);
                    //  int vtd= vertsList.IndexOf(vert);
                    int vtd = vertsList.FindIndex(r => r.name.Equals(vert.name));
                    //  vertsList[vtd] = new Vert();
                    vertsList[vtd].addNeighbour(new Edge(1, vertsList[vtd], vertsList[vt]));// nối đoạn thẳng
                }
            }
            // tính hàm đánh giá
            Vert beginVert  = new Vert(nameBeginVert);
            int  vt1        = vertsList.FindIndex(v => v.name.Equals(beginVert.name));
            Vert targetVert = new Vert(nameTargetVert);
            int  vt2        = vertsList.FindIndex(v => v.name.Equals(targetVert.name));

            PathFinder shortestPath = new PathFinder();

            shortestPath.ShortestP(vertsList[vt1]);
            try {
                double a = vertsList[vt2].getDist();
                return(a);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(Double.MaxValue);
            }
        }
 public void setPr(Vert pr)
 {
     this.pr = pr;
 }
 public void setTargetVert(Vert targetVert)
 {
     this.targetVert = targetVert;
 }
 public void setStartVert(Vert startVert)
 {
     this.startVert = startVert;
 }
 public Edge(double weight, Vert startVert, Vert targetVert)
 {
     this.weight     = weight;
     this.startVert  = startVert;
     this.targetVert = targetVert;
 }