public static Edge findEdge(Edge[] edges, Node node1, Node node2) { for (int i = 0; i < edges.Length; i++) if (edges[i].startNode.label.Text == node1.label.Text && edges[i].endNode.label.Text == node2.label.Text) return edges[i]; return null; }
public static void highlightEdge(PictureBox panel, Edge[] edges, Node node1, Node node2) { //Edge edge = findEdge(edges, node1, node2); Edge edge = new Edge(ref node1, ref node2, findEdge(edges, node1, node2).weightEdge); edge.colorEdge = Color.Green; edge.drawEdge(panel); }
public Graph(DataGridView inputMatrix, int[][] data ) { this.data = data; fillMatrix(inputMatrix); point = SystemFunction.polarPoint(data, 130); /*---------------------Create Node-------------------------------*/ this.nodes = new Node[data.Length]; for (int i = 0; i < data.Length; i++) nodes[i] = new Node(point[i], "INF"); /*---------------------------------------------------------------*/ /*---------------------------------------------------------------*/ /*----------------------Create edges-----------------------------*/ this.numberOfEdges = SystemFunction.numberEdge(data); this.edges = new Edge[numberOfEdges]; int k = 0; for (int i = 0; i < data.Length; i++) for (int j = 0; j < data.Length; j++) if (data[i][j] != -1) { edges[k] = new Edge(ref nodes[i],ref nodes[j], data[i][j]); k++; } /*---------------------------------------------------------------*/ }
public static bool checkFreePlace(Node[] nodes, float x, float y) { for (int i = 0; i < nodes.Length; i++) if (x >= nodes[i].location.X - size && x <= nodes[i].location.X + size && y <= nodes[i].location.Y + size && y >= nodes[i].location.Y - size) return false; return true; }
public static Node findNodeWithCoord(Node[] nodes, float x, float y) { for (int i = 0; i < nodes.Length; i++) { if(x >= nodes[i].location.X && x <= nodes[i].location.X + size && y <= nodes[i].location.Y + size && y >= nodes[i].location.Y) return nodes[i]; } return null; }
public void addNode(Node n) { Array.Resize(ref nodes, nodes.Length + 1); nodes[nodes.Length - 1] = n; matrix.Columns.Add("c" + n.label.Text, n.label.Text); for (int i = 0; i < nodes.Length; i++) matrix.Columns[i].Width = matrix.Width / (nodes.Length + 1); matrix.Rows.Add(); matrix.Rows[nodes.Length - 1].HeaderCell.Value = n.label.Text; }
public Edge(ref Node node1, ref Node node2, int weight) { this.startNode = node1; this.endNode = node2; this.weightEdge = weight; this.label = new Label(); this.label.AutoSize = true; this.label.Text = Convert.ToString(weight); this.label.Size = new Size(15, 15); this.label.Location = new Point((int)((startNode.location.X + endNode.location.X) / 2), (int)((startNode.location.Y + endNode.location.Y) / 2)); colorEdge = Color.Coral; }
// public DataGridView inputMatrix; public Graph(Node[] nodes, Edge[] edges) { this.nodes = nodes; this.edges = edges; data = new int[nodes.Length][]; for (int i = 0; i < data.Length; i++) data[i] = new int[data.Length]; for (int i = 0; i < data.Length; i++) for (int j = 0; j < data.Length; j++) data[i][j] = -1; for(int i = 0; i < edges.Length; i++) data[Convert.ToInt32(edges[i].startNode.label.Text)] [Convert.ToInt32(edges[i].endNode.label.Text)] = edges[i].weightEdge; }
public static int[] deijkstra(PictureBox pictureBox1, int currNode, ref Edge[] edge, ref Node[] node, int[][] graph) { int size = node.Length; int[][]visitedNode = new int[size][]; for (int i = 0; i < size; i++) visitedNode[i] = new int[2]; /*visitedNode[0] - current value of label, visitedNode[1] - 1 or 0. visited or not visited accordingly*/ for (int i = 0; i < size; i++) visitedNode[i][0] = Int32.MaxValue; visitedNode[currNode][0] = 0; node[currNode].label.Text = "0"; node[currNode].label.Refresh(); do { for (int i = 0; i < size; i++) if (graph[currNode][i] != -1 && visitedNode[i][1] != 1) // check if the nodes(currNode and i) are connected { Edge.highlightEdge(pictureBox1, edge, node[currNode], node[i]); int possible = visitedNode[currNode][0] + graph[currNode][i]; if (visitedNode[i][0] > possible) { visitedNode[i][0] = possible; node[i].label.Text = Convert.ToString(possible); count++; } node[i].highLight(pictureBox1, Color.YellowGreen); node[i].label.Refresh(); Thread.Sleep(500); } visitedNode[currNode][1] = 1; node[currNode].highLight(pictureBox1, Color.DarkGreen); currNode = findNextNode(visitedNode); }while(currNode != -1); int[] answer = new int[size]; for (int i = 0; i < size; i++) answer[i] = visitedNode[i][0]; return answer; }
public void highLight(PictureBox panel, Color color) { Node node = new Node(this.location, this.label.Text); node.Color = color; node.label.BackColor = color; node.drawNode(panel); node.label.Refresh(); }
public Edge createEdge(Node[] chooseNodes, int weigth) { PointF point1 = new PointF(x1, y1); PointF point2 = new PointF(x2, y2); Edge edge = new Edge(ref chooseNodes[0],ref chooseNodes[1], weigth); addEdge(edge); Node.findNodeWithCoord(nodes, x1, y1).label.Text = choose1; Node.findNodeWithCoord(nodes, x2, y2).label.Text = choose2; startOrEndOfEdge = 0; return edge; }
public Node createNode(float x, float y, int n) { if (Node.checkFreePlace(nodes, x , y) == true) { PointF point = new PointF(x , y); Node node = new Node(point, Convert.ToString(n)); addNode(node); return node; } return null; }