Beispiel #1
0
 public void VertexHashCodeTest()
 {
     Vertex v1 = new Vertex("vertex1");
     Vertex v2 = new Vertex("vertex2");
     Assert.AreEqual((new Vertex("vertex1")).GetHashCode(), v1.GetHashCode(), "Objects of same name have different hashCode (vertex1).");
     Assert.AreEqual((new Vertex("vertex2")).GetHashCode(), v2.GetHashCode(), "Objects of same name have different hashCode (vertex2).");
 }
Beispiel #2
0
        public void EdgeEqualsTest()
        {
            Vertex u = new Vertex("vertex1");
            Vertex v = new Vertex("vertex2");
            Vertex w = new Vertex("vertex3");
            const int five = 5;
            const int six = 6;

            Edge uv = new Edge(u, v);
            Edge vw = new Edge(v, w);
            Edge wu = new Edge(w, u);
            Edge vu = new Edge(v, u);

            Edge uv5 = new Edge(u, v, five);
            Edge uv6 = new Edge(u, v, six);
            Edge vu5 = new Edge(v, u, five);
            Edge vu6 = new Edge(v, u, six);

            Assert.AreEqual(new Edge(u, v), uv);
            Assert.AreEqual(new Edge(u, v, five), uv);
            Assert.AreEqual(new Edge(u, v, five), uv5);
            Assert.AreEqual(new Edge(u, v, six), uv5);

            Assert.AreEqual(new Edge(new Vertex("vertex1"), new Vertex("vertex2")), uv);

            Assert.AreNotEqual(uv, vw);
            Assert.AreNotEqual(uv, vu);
            Assert.AreNotEqual(vw, vu);
            Assert.AreNotEqual(uv5, vw);
            Assert.AreNotEqual(uv5, vu);

            Assert.AreNotEqual(five, uv5);
            Assert.AreNotEqual(uv5, five);
        }
Beispiel #3
0
        public void VertexToStringTest()
        {
            Vertex v1 = new Vertex("vertex1");
            Vertex v2 = new Vertex("vertex2");

            Assert.AreEqual("Vertex[vertex1]", v1.ToString(), "ToString Method should generate Name (vertex1).");
            Assert.AreEqual("Vertex[vertex2]", v2.ToString(), "ToString Method should generate Name (vertex2).");
        }
Beispiel #4
0
 public void VertexEqualsTest()
 {
     Vertex v1 = new Vertex("vertex1");
     Vertex v2 = new Vertex("vertex2");
     Assert.IsTrue(v1.Equals(new Vertex("vertex1")), "v1 should be equal to new vertex with same name.");
     Assert.IsTrue(v2.Equals(new Vertex("vertex2")), "v2 should be equal to new vertex with same name.");
     Assert.IsFalse(v1.Equals(v2), "v1 and v2 should not be equal since they have different names.");
     Assert.IsFalse(v1.Equals("string"), "Vertex should not equal object of different type");
 }
 //Проверка на наличие вершины в списке
 public static bool IsConVerx(List<Vertex> Q, Vertex v)
 {
     bool End = false;
     for (int i = 0; i < Q.Count; i++)
     {
         if (Q[i] == v) End = true;
     }
     if (End) return true;
     return false;
 }
Beispiel #6
0
        public void EdgeHashCodeTest()
        {
            Vertex u = new Vertex("vertex1");
            Vertex v = new Vertex("vertex2");

            Edge uv = new Edge(u, v);
            Edge vu = new Edge(v, u);

            Assert.AreEqual((new Edge(u, v)).GetHashCode(), uv.GetHashCode());
            Assert.AreEqual((new Edge(v, u)).GetHashCode(), vu.GetHashCode());
        }
 public Vertex(double x, double y)
 {
     this.x = x;
     this.y = y;
     t = float.MaxValue;
     p = float.MaxValue;
     prev = null;
     isin = false;
     wasMin = false;
     rad = 7;
 }
 //Находение ребра в списке ребер по заданным вершинам
 public Edge FindEdge(Vertex v1, Vertex v2)
 {
     Edge temp = null;
     for (int j = 0; j < E.Count; j++)
     {
         if ((E[j].V1 == v1 && E[j].V2 == v2) || (E[j].V2 == v1 && E[j].V1 == v2))
         {
             temp = E[j];
         }
     }
     return temp;
 }
 //
 public void ChangeMarkerT2(List<Vertex> l, Vertex temp)
 {
     double w = 0;
     for (int i = 0; i < l.Count; i++)
     {
         w = (float)FindEdge(temp, l[i]).Weight;
         if (l[i].t > w)
         {
             l[i].t = (float)w;
             l[i].prev = temp;
         }
     }
 }
Beispiel #10
0
        public void EdgeToStringTest()
        {
            Vertex u = new Vertex("vertex1");
            Vertex v = new Vertex("vertex2");
            const int five = 5;

            Edge uv = new Edge(u, v);
            Edge vu = new Edge(v, u);
            Edge uv5 = new Edge(u, v, five);

            Assert.AreEqual("Edge(vertex1,vertex2)", uv.ToString());
            Assert.AreEqual("Edge(vertex1,vertex2)", uv5.ToString());
            Assert.AreEqual("Edge(vertex2,vertex1)", vu.ToString());
        }
Beispiel #11
0
        public void EdgeConstructorTest()
        {
            Vertex u = new Vertex("vertex1");
            Vertex v = new Vertex("vertex2");
            const int w = 5;

            Edge e1 = new Edge(u, v);
            Edge e2 = new Edge(u, v, w);

            Assert.AreEqual(u, e1.u);
            Assert.AreEqual(v, e1.v);
            Assert.IsFalse(e1.weight.HasValue);

            Assert.AreEqual(u, e2.u);
            Assert.AreEqual(v, e2.v);
            Assert.IsTrue(e2.weight.HasValue);
            Assert.AreEqual(w, e2.weight);
        }
Beispiel #12
0
        public static Graph FromTikz(string tikz)
        {
            var idToVertex = new Dictionary <string, Vertex>();

            foreach (Match match in VertexRegex.Matches(tikz))
            {
                if (match.Groups.Count != 5)
                {
                    return(null);
                }

                double x;
                if (!double.TryParse(match.Groups[1].Value, out x))
                {
                    return(null);
                }
                double y;
                if (!double.TryParse(match.Groups[2].Value, out y))
                {
                    return(null);
                }
                var label = match.Groups[3].Value ?? "";
                var id    = match.Groups[4].Value;
                if (string.IsNullOrEmpty(id))
                {
                    return(null);
                }

                idToVertex[id] = new Vertex(x, 1.0 - y, label.Trim('$'));
            }

            var edges = new List <Edge>();

            foreach (Match match in EdgeRegex.Matches(tikz))
            {
                if (match.Groups.Count != 4)
                {
                    return(null);
                }

                var style = match.Groups[1].Value ?? "";
                var id1   = match.Groups[2].Value;
                var id2   = match.Groups[3].Value;

                if (string.IsNullOrEmpty(id1) || string.IsNullOrEmpty(id2))
                {
                    return(null);
                }

                var orientation = Edge.Orientations.None;
                if (style.Contains("pre"))
                {
                    orientation = Edge.Orientations.Backward;
                }
                else if (style.Contains("post"))
                {
                    orientation = Edge.Orientations.Forward;
                }


                edges.Add(new Edge(idToVertex[id1], idToVertex[id2], orientation));
            }

            return(new Graph(idToVertex.Values, edges));
        }
Beispiel #13
0
 public void VertexConstructorTest()
 {
     Vertex v = new Vertex("vertex1");
     Assert.AreEqual("vertex1", v.Name, "The created vertex does not have the specified name.");
 }
 //Формирование списка вершин в минимальном остовном дереве/графе оптимального пути
 public List<Vertex> IsConList(Vertex begin)
 {
     List<Vertex> Q = new List<Vertex>();
     Q.Remove(begin);
     Q.Insert(0, begin);
     for (int i = 0; i < V.Count; i++)
     {
         V[i].cocheck = 1;
     }
     Q[0].cocheck = 2;
     while (IsConPrior(2))
     {
         for (int i = 0; i < V.Count; i++)
         {
             if (V[i].cocheck == 2)
             {
                 List<Vertex> n = new List<Vertex>();
                 V[i].cocheck = 3;
                 Q.Add(V[i]);
                 FindingNeighbours(n, V[i]);
                 for (int j = 0; j < n.Count; j++)
                 {
                     if (n[j].cocheck == 1)
                         n[j].cocheck = 2;
                 }
             }
         }
     }
     return Q;
 }
 public void FindMinSpanTree(Vertex first)
 {
     first.t = 0;
     Vertex temp = first;
     List<Vertex> neighbours = new List<Vertex>();
     List<Vertex> Q = new List<Vertex>();
     while (Gone())
     {
         temp = Mint();
         temp.p = temp.t;
         neighbours = new List<Vertex>();
         FindingNeighbours1(neighbours, temp);
         ChangeMarkerT2(neighbours, temp);
         Q.Add(temp);
     }
     for (int i = 1; i < Q.Count; i++)
     {
         Edge e = FindEdge(Q[i], Q[i].prev);
         e.optimal = true;
     }
 }
 //Нахождение у вершины соседей с максимальным временным приоритетом
 public void FindingNeighbours1(List<Vertex> l, Vertex temp)
 {
     for (int j = 0; j < E.Count; j++)
     {
         if (E[j].V1 == temp && E[j].V2.p == max)
         {
             l.Add(E[j].V2);
         }
         else if (E[j].V2 == temp && E[j].V1.p == max)
         {
             l.Add(E[j].V1);
         }
     }
 }
Beispiel #17
0
 public Edge(Vertex u, Vertex v, int weight)
 {
     this.weight = weight;
     this.u = u;
     this.v = v;
 }
Beispiel #18
0
 public Edge(Vertex u, Vertex v)
 {
     this.u = u;
     this.v = v;
 }
 public Edge(Vertex V1, Vertex V2)
 {
     this.V1 = V1;
     this.V2 = V2;
     Weight = Math.Sqrt(Math.Pow((V1.x - V2.x), 2) + Math.Pow((V1.y - V2.y), 2));
 }
Beispiel #20
0
        public bool EdgeExists(Vertex v1, Vertex v2)
        {
            Edge edge;

            return(EdgeExists(v1, v2, out edge));
        }
 public void FindingMinWay(Vertex first, Vertex last)
 {
     first.t = 0;
     Vertex temp;
     List<Vertex> neighbours = new List<Vertex>();
     while (last.p == max)
     {
         temp = Mint();
         temp.p = temp.t;
         neighbours = new List<Vertex>();
         FindingNeighbours1(neighbours, temp);
         ChangeMarkerT(neighbours, temp);
     }
     temp = last;
     while (temp != first)
     {
         neighbours = new List<Vertex>();
         FindingNeighbours(neighbours, temp);
         for (int i = 0; i < neighbours.Count; i++)
         {
             double w = FindEdge(temp, neighbours[i]).Weight;
             if (Math.Round(temp.p - neighbours[i].p, 3) == Math.Round(w, 3))
             {
                 FindEdge(temp, neighbours[i]).optimal = true;
                 temp = neighbours[i];
                 break;
             }
         }
     }
 }
Beispiel #22
0
 public void AddEdge(Vertex <T> a, Vertex <T> b)
 {
     b.AddNeighbors(a);
     a.AddNeighbors(b);
 }
 private void Form1_MouseDown(object sender, MouseEventArgs e)
 {
     for (int i = 0; i < G.V.Count; i++)
     {
         if (G.V[i].Clicked(e.X, e.Y))
         {
             temp1 = G.V[i];
             clicked = true;
             moving = false;
             break;
         }
     }
     if (!clicked)
     {
         moving = true;
         movestart.X = e.X;
         movestart.Y = e.Y;
     }
     for (int i = 0; i < G.E.Count; i++)
     {
         G.E[i].optimal = false;
     }
 }
Beispiel #24
0
 public bool AddEdge(Vertex v1, Vertex v2, int multiplicity = 1)
 {
     return(AddEdge(v1, v2, Edge.Orientations.None, multiplicity));
 }