Exemple #1
0
        public void AddEdge(Vertex partner, int weight)
        {
            Edges.Add(new Edge(this, partner, weight));

            if (!partner.Edges.Contains(Edges.LastOrDefault()))
            {
                partner.AddEdge(Edges.LastOrDefault());
            }
        }
Exemple #2
0
        public bool AreConnect(Vertex v1, Vertex v2, List<Vertex> tree, List<Edge> visited)
        {
            bool connected = false;
            Queue<Vertex> queue = new Queue<Vertex>();
            List<Edge> visitedEdges = new List<Edge>();
            queue.Enqueue(v1);

            while(queue.Count > 0)
            {
                Vertex temp = queue.Dequeue();

                if (temp == v2)
                {
                    connected = true;
                    break;
                }

                foreach (Edge e in temp.Edges)
                {
                    if(visited.Contains(e))
                    {
                        if (!visitedEdges.Contains(e))
                        {
                            visitedEdges.Add(e);

                            if (e.Vertex1 == temp)
                            {
                                queue.Enqueue(e.Vertex2);
                            }
                            else
                            {
                                queue.Enqueue(e.Vertex1);
                            }
                        }
                    }
                }
            }
            return connected;
        }
Exemple #3
0
 private void DFS(Vertex v)
 {
     v.visited = true; //将访问标志设为true
      //Console.Write(v.data + " "); //访问
      dfsPath.Append(v.data + " ");
      Node node = v.firstEdge;
      while (node != null) //访问此顶点的所有邻接点
      {
          //如果邻接点未被访问,则递归访问它的边
          if (!node.adjvex.visited)
          {
              DFS(node.adjvex); //递归
          }
          node = node.next; //访问下一个邻接点
      }
 }
Exemple #4
0
        //添加有向边
        private void AddDirectedEdge(Vertex fromVer, Vertex toVer)
        {
            if (fromVer.firstEdge == null) //无邻接点时
            {
                fromVer.firstEdge = new Node(toVer);
            }
            else
            {
                Node tmp, node = fromVer.firstEdge;
                do
                {   //检查是否添加了重复边
                    if (node.adjvex.data.Equals(toVer.data))
                    {
                        throw new ArgumentException("添加了重复的边!");
                    }

                    tmp = node;
                    node = node.next;
                } while (node != null);
                tmp.next = new Node(toVer); //添加到链表未尾
            }
        }
		public int GetIndexOfVertex(Vertex v1)
		{
			for (int V = 0; V < Vertices.Length; V++) {
				if (v1.label == Vertices [V].label) {
					return V;
				} 
					

			}
			return -1;
		}
Exemple #6
0
 public Edge(Vertex vertex, Vertex partner, int weight)
 {
     this.Vertex1 = vertex;
     this.Vertex2 = partner;
     this.Weight = weight;
 }
        private static void ConsoleWriteFromTo(Func <Vertex, Vertex, List <Vertex> > function, Vertex from, Vertex to)
        {
            Console.WriteLine($"Путь из {from} в {to}");
            var tmp = function(from, to);

            if (tmp == null || tmp.Count == 0)
            {
                Console.Write("Нет такого пути!!");
            }
            else
            {
                foreach (var item in function(from, to))
                {
                    Console.Write($"{item},");
                }
            }

            Console.WriteLine();
        }
Exemple #8
0
 /// <summary>
 /// 頂点を追加する
 /// </summary>
 /// <param name="v"></param>
 public void AddVertex(Vertex v)
 {
     Vertices.Add(v);
     InEdges.Add(new List<Edge>());
     OutEdges.Add(new List<Edge>());
 }
Exemple #9
0
 public void LinkVertex(Vertex v1, Vertex v2, int weight)
 {
     v1.AddEdge(v2, weight);
     Edges.Add(v1.Edges.LastOrDefault());
 }
Exemple #10
0
 public void LinkVertex(Vertex v1, Vertex v2)
 {
     v1.AddEdge(v2);
     Edges.Add(v1.Edges.LastOrDefault());
 }
Exemple #11
0
 public void InsertVertex(Vertex vertex)
 {
     if (Vertices.Count == 0)
     {
         Root = vertex;
     }
     Vertices.Add(vertex);
 }
Exemple #12
0
 public Edge(Vertex <T> from, Vertex <T> to, int weight = 1)
 {
     v = from;
     u = to;
     w = weight;
 }
		public void ShowEdgesOfVertex(Vertex v)
		{
			int thisIndex = GetIndexOfVertex (v);
			Console.WriteLine (" Vertex Data is " + v.label);
			Console.WriteLine (" Edges are ");
			for (int i = 0; i < Edges.GetLength(0); i++) {
				if (Edges [thisIndex, i] == 1)
					Console.Write (GetVertexAtIndex (i).label + " ,");
			}
		}
		public void AddEdge(Vertex v1, Vertex v2)
		{
			int indexV1 = GetIndexOfVertex (v1);
			int indexV2 = GetIndexOfVertex (v2);
			Edges [indexV1, indexV2] = 1;
			Edges [indexV2, indexV1] = 1;
		}
Exemple #15
0
        private static void init_algo(List <int> distance, List <int> path, List <adjElement> adj_list, Vertex start_vertex)
        {
            int index_start_vertex = get_vertex_index(adj_list, start_vertex);

            if (index_start_vertex == -1)
            {
                return;
            }
            for (int i = 0; i < adj_list[index_start_vertex].Adj_vertices.Count; i++)
            {
                int index_cur_to_vertex = get_vertex_index(adj_list, adj_list[index_start_vertex].Adj_vertices[i].Vertex);
                distance[index_cur_to_vertex] = adj_list[index_start_vertex].Adj_vertices[i].Lenght;
                path[index_cur_to_vertex]     = index_start_vertex;
            }

            distance[index_start_vertex] = 0;
        }
Exemple #16
0
        private static List <Vertex> get_short_path(List <adjElement> adj_list, Vertex start_vertex, Vertex end_vertex, List <int> path)
        {
            List <Vertex> short_path  = new List <Vertex>();
            int           start_index = get_vertex_index(adj_list, start_vertex);
            int           end_index   = get_vertex_index(adj_list, end_vertex);

            while (path[end_index] != start_index)
            {
                short_path.Add(adj_list[end_index].Vertex);
                end_index = path[end_index];
            }
            short_path.Add(adj_list[end_index].Vertex);
            short_path.Add(adj_list[start_index].Vertex);
            return(short_path);
        }
Exemple #17
0
        public void RemoveEdgesFromVertex(Vertex vertex)
        {
            if (vertex == null)
            {
                return;
            }

            vertex.Edges.Clear();
        }
Exemple #18
0
        public Node next; //下一个邻接点指针域

        #endregion Fields

        #region Constructors

        public Node(Vertex value)
        {
            adjvex = value;
        }
Exemple #19
0
        public void RemoveVertex(Vertex vertex)
        {
            if (vertex == null)
            {
                return;
            }

            foreach (Edge edge in vertex.Edges)
            {
                Vertex partner = null;
                if (edge.Vertex1 == vertex)
                {
                    partner = edge.Vertex2;
                }
                else
                {
                    partner = edge.Vertex1;
                }

                partner.Edges.Remove(edge);
            }

            RemoveEdgesFromVertex(vertex);
            Vertices.Remove(vertex);
        }
Exemple #20
0
 public Edge(Vertex vertex, Vertex partner)
 {
     Vertex1 = vertex;
     Vertex2 = partner;
 }
Exemple #21
0
        public Dictionary<Vertex, int> ShortestPath(Vertex start)
        {
            Queue<Vertex> queue = new Queue<Vertex>();
            List<Edge> visitedEdges = new List<Edge>();
            List<Vertex> visitedVertices = new List<Vertex>();
            Dictionary<Vertex, int> costs = new Dictionary<Vertex, int>();

            costs.Add(start, 0);
            queue.Enqueue(start);

            while (queue.Count > 0)
            {
                Vertex temp = queue.Dequeue();
                visitedVertices.Add(temp);

                foreach (Edge e in temp.Edges)
                {
                    if (!visitedEdges.Contains(e))
                    {
                        visitedEdges.Add(e);
                    }

                        if (e.Vertex1 == temp)
                        {
                            if (!costs.ContainsKey(e.Vertex2) || (costs[e.Vertex2] > costs[temp] + e.Weight))
                            {
                                costs[e.Vertex2] = costs[temp] + e.Weight;
                                queue.Enqueue(e.Vertex2);
                            }
                        }
                        else
                        {
                            if (!costs.ContainsKey(e.Vertex1) || (costs[e.Vertex1] > costs[temp] + e.Weight))
                            {
                                costs[e.Vertex1] = costs[temp] + e.Weight;
                                queue.Enqueue(e.Vertex1);
                            }
                        }
                }
            }
            return costs;
        }
Exemple #22
0
        public Creator(int[,] smatrix, bool isDigraph)
        {
            //Проверка данных
            if (smatrix.GetLength(0) != smatrix.GetLength(1))
            {
                throw new ArgumentException();
            }
            int length = smatrix.GetLength(0);

            if (length < 1 || length > 10)
            {
                throw new ArgumentException("Количество вершин некорректно");
            }
            painter?.Dispose();

            //Вершины и объекты
            Graph.Vertex[] vertices = new Graph.Vertex[length];
            List <Shape>   shapes   = new List <Shape>();

            //Инициализация вершин
            for (int i = 0; i < length; i++)
            {
                vertices[i] = new Graph.Vertex()
                {
                    Name = nameVertex[i]
                };
                shapes.Add(vertices[i]);
            }
            //Проведем связи (ребра, кольца)
            if (isDigraph)
            {
                //Если граф орентированный
                for (int i = 0; i < length; i++)
                {
                    for (int j = 0; j < length; j++)
                    {
                        if (smatrix[i, j] > 0)
                        {
                            if (i != j)
                            {
                                shapes.Add(vertices[i].SetConnect(vertices[j], TypeConnect.DigraphOwn, smatrix[i, j]));
                            }
                            else
                            {
                                shapes.Add(vertices[i].SetConnect(vertices[i], TypeConnect.Diloop, smatrix[i, j]));
                            }
                        }
                    }
                }
            }
            else
            {
                //Если граф неорентированный
                for (int i = 0; i < length; i++)
                {
                    for (int j = 0; j < length; j++)
                    {
                        if (smatrix[i, j] > 0)
                        {
                            if (i != j)
                            {
                                shapes.Add(vertices[i].SetConnect(vertices[j], TypeConnect.Edge, smatrix[i, j]));
                            }
                            else
                            {
                                shapes.Add(vertices[i].SetConnect(vertices[i], TypeConnect.Loop, smatrix[i, j]));
                            }
                        }
                    }
                }
            }
            shapes.RemoveAll(item => item == null);
            //Размер абстракного блока на плоскости экрана(Размер вешины)
            int size = 60;

            //Передаем все объекты которые нужно отрисовать отрисовщику, их размер и матрицу их визуального расположениея
            switch (length)
            {
            case 1:
                painter = new Painter(shapes, size, new object[, ]
                {
                    { 'A' },
                });
                break;

            case 2:
                painter = new Painter(shapes, size, new object[, ]
                {
                    { 'A', '0', 'B' },
                });
                break;

            case 3:
                painter = new Painter(shapes, size, new object[, ]
                {
                    { 'A', '0', 'B' },
                    { '0', '0', '0' },
                    { '0', 'C', '0' },
                });
                break;

            case 4:
                painter = new Painter(shapes, size, new object[, ]
                {
                    { 'A', '0', 'B' },
                    { '0', '0', '0' },
                    { 'C', '0', 'D' },
                });
                break;

            case 5:
                painter = new Painter(shapes, size, new object[, ]
                {
                    { 'A', '0', '0', '0', 'B' },
                    { '0', '0', 'C', '0', '0' },
                    { 'D', '0', '0', '0', 'E' },
                });
                break;

            case 6:
                painter = new Painter(shapes, size, new object[, ]
                {
                    { 'A', '0', '0', '0', 'B' },
                    { '0', 'C', '0', 'D', '0' },
                    { 'E', '0', '0', '0', 'F' },
                });
                break;

            case 7:
                painter = new Painter(shapes, size, new object[, ]
                {
                    { '0', '0', 'C', '0', '0' },
                    { 'A', '0', '0', '0', 'B' },
                    { '0', 'D', '0', 'E', '0' },
                    { 'F', '0', '0', '0', 'G' },
                });
                break;

            case 8:
                painter = new Painter(shapes, size, new object[, ]
                {
                    { '0', '0', 'C', '0', '0' },
                    { 'A', '0', '0', '0', 'B' },
                    { '0', 'D', '0', 'E', '0' },
                    { 'F', '0', '0', '0', 'G' },
                    { '0', '0', 'H', '0', '0' },
                });
                break;

            case 9:
                painter = new Painter(shapes, size, new object[, ]
                {
                    { '0', '0', 'C', '0', '0' },
                    { 'A', '0', '0', '0', 'B' },
                    { '0', 'D', '0', 'E', '0' },
                    { 'F', '0', '0', '0', 'G' },
                    { '0', 'H', '0', 'I', '0' },
                });
                break;

            case 10:
                painter = new Painter(shapes, size, new object[, ]
                {
                    { '0', 'B', '0', 'C', '0' },
                    { 'A', '0', '0', '0', 'D' },
                    { '0', 'E', '0', 'F', '0' },
                    { 'G', '0', '0', '0', 'H' },
                    { '0', 'I', '0', 'J', '0' },
                });
                break;

            default:
                break;
            }
        }
		public void AddVertex(string lbl)
		{
			if (current < Vertices.Length) {
				Vertex v = new Vertex (lbl);
				Vertices [current] = v;
				current++;

			}
		}