public void AddRow(Form1.edge myEdge) { TextBox cell1 = new TextBox(); TextBox cell2 = new TextBox(); TextBox cell3 = new TextBox(); Button cell4 = new Button(); cell1.Dock = cell2.Dock = cell3.Dock = cell4.Dock = DockStyle.Fill; cell1.ReadOnly = cell2.ReadOnly = true; cell4.Height = 20; cell4.Anchor = AnchorStyles.Top; cell4.BackColor = Color.Red; cell4.Text = "X"; cell4.TextAlign = ContentAlignment.MiddleCenter; cell4.Enabled = true; cell4.FlatStyle = FlatStyle.Flat; cell4.Click += this.Delete_Click; cell3.Enter += this.Focus_Enter; cell3.Leave += this.Focus_Leave; cell1.Text = myEdge.firstNode.Text; tableLayoutPanel1.Controls.Add(cell1); cell2.Text = myEdge.secondNode.Text; tableLayoutPanel1.Controls.Add(cell2); cell3.Text = myEdge.weight.ToString(); tableLayoutPanel1.Controls.Add(cell3); tableLayoutPanel1.Controls.Add(cell4); }
private void Focus_Leave(object sender, EventArgs e) { //get modified weight of the edge var cell = sender as TextBox; if (cell.Text == null) { cell.Text = "0"; } newWeight = convertToNumber(cell.Text); //overwrite cell with converted weight cell.Text = newWeight.ToString(); //if the weight has been changed, update the edges if (newWeight != previousWeight) { var position = tableLayoutPanel1.GetPositionFromControl(cell); int row = position.Row - 1; Form1.edge replacement = new Form1.edge( Form1.edges[row].firstNode, Form1.edges[row].secondNode, newWeight ); Form1.edges[row] = replacement; } //then update the graphics of the graph Form1.redrawEdges(Color.WhiteSmoke); }
private void timerPrimPseudo_Tick(object sender, EventArgs e) { step++; //increase the step if (step <= nrLines) //if code has not finished, proceed and run the algorithm { if (step >= 1) //remove highlight from current line of code { lineCode[step].BackColor = this.BackColor; } //and execute the current line of code if (step == 1) { //extract the start node startNode = Int32.Parse(textBox2.Text); Vmin = startNode; //mark the start node as visited viz[startNode] = true; addedNodes++; //highlight the node Form1.nodes[startNode].BackColor = Color.Orange; } else if (step == 2) { //if we visited all nodes, STOP the algorithm if (addedNodes == Form1.nrNodes) { step = 6; } //else, proceed with the repetitive loop } else if (step == 3) { //we consider the unvisited node (NVmin) situated at the minimum distance from an already visited node (Vmin) int min = int.MaxValue; for (int i = 0; i < Form1.edges.Count(); i++) { int n1 = Int32.Parse(Form1.edges[i].firstNode.Text); int n2 = Int32.Parse(Form1.edges[i].secondNode.Text); if (viz[n1] == true && viz[n2] == false) { if (Form1.edges[i].weight < min) { min = Form1.edges[i].weight; mch = Form1.edges[i]; NVmin = n2; } } else if (viz[n1] == false && viz[n2] == true) { if (Form1.edges[i].weight < min) { min = Form1.edges[i].weight; mch = Form1.edges[i]; NVmin = n1; } } } //highlight edge and NVmin Form1.DrawEdge(mch, Color.Yellow); Form1.nodes[NVmin].BackColor = Color.Yellow; } else if (step == 4) { //edge NVmin-Vmin gets added to the tree Form1.DrawEdge(mch, Color.Red); } else if (step == 5) { //NVmin is marked as visited addedNodes++; viz[NVmin] = true; Form1.nodes[NVmin].BackColor = Color.Orange; //jump back to the start of the repetitive structure step = 1; } if (step < nrLines) //highlight next line of code, if exists { lineCode[step + 1].BackColor = Color.Orange; } } else if (step > nrLines) //else, if the code has finished, stop the timer { timerPrimPseudo.Enabled = false; startStop.Enabled = false; stepByStep.Enabled = false; } }
private void timerKruskalPseudo_Tick(object sender, EventArgs e) { step++; //increase the step if (step <= nrLines) //if code has not finished, proceed and run the algorithm { if (step >= 1) //remove highlight from current line of code { lineCode[step].BackColor = this.BackColor; } //and execute the current line of code if (step == 1) { //sort the edges in increasing order of weights for (int i = 0; i < Form1.edges.Count() - 1; i++) { for (int j = i + 1; j < Form1.edges.Count(); j++) { if (Form1.edges[i].weight > Form1.edges[j].weight) { Form1.edge aux = new Form1.edge(Form1.edges[i].firstNode, Form1.edges[i].secondNode, Form1.edges[i].weight); Form1.edges[i] = Form1.edges[j]; Form1.edges[j] = aux; } } } //and update Form Edges Form1.frmEdges.FormEdges_VisibleChanged(this, e); } else if (step == 2) { //put every node in a separate disjoint set for (int i = 1; i <= Form1.nrNodes; i++) { M[Form1.nodes[i]] = i; //give each node a random color Color randomColor = Color.FromArgb(50 + rand.Next(206), 50 + rand.Next(206), 50 + rand.Next(206)); Form1.nodes[i].BackColor = randomColor; } } else if (step == 3) { //if we already have enough edges in MST (nr_nodes - 1), we STOP if (inMST.Count() == Form1.nrNodes - 1) { step = 8; } //else, we proceed with the repetitive structure } else if (step == 4) { //extract the edge with the next minimum weight (next edge in the sorted list) currEdge++; Form1.DrawEdge(Form1.edges[currEdge], Color.Yellow); } else if (step == 5) { //if the extremitites of the extracted edge are in the same set, ignore this edge if (M[Form1.edges[currEdge].firstNode] == M[Form1.edges[currEdge].secondNode]) { Form1.DrawEdge(Form1.edges[currEdge], Color.Black); step = 7; } //else, we proceed with the union of the two sets } else if (step == 6) { //add the extracted edge in the MST Form1.DrawEdge(Form1.edges[currEdge], Color.Red); inMST.Add(Form1.edges[currEdge]); } else if (step == 7) { //set A (of one extremity) and set B (of the other extremity), with A < B int A, B; Color myColor = new Color(); //color to change the nodes of one set to if (M[Form1.edges[currEdge].firstNode] < M[Form1.edges[currEdge].secondNode]) { A = M[Form1.edges[currEdge].firstNode]; B = M[Form1.edges[currEdge].secondNode]; myColor = Form1.edges[currEdge].firstNode.BackColor; } else { A = M[Form1.edges[currEdge].secondNode]; B = M[Form1.edges[currEdge].firstNode]; myColor = Form1.edges[currEdge].secondNode.BackColor; } //union the two disjoint sets (all nodes in B get into A) for (int i = 1; i <= Form1.nrNodes; i++) { if (M[Form1.nodes[i]] == B) { M[Form1.nodes[i]] = A; Form1.nodes[i].BackColor = myColor; } } } else if (step == 8) { //jump back to the start of the repetitive loop step = 2; } if (step < nrLines) //highlight next line of code, if exists { lineCode[step + 1].BackColor = Color.Orange; } } else if (step > nrLines) //else, if the code has finished, stop the timer and reset the algorithm { timerKruskalPseudo.Enabled = false; //disable playback buttons startStop.Enabled = false; stepByStep.Enabled = false; } }