private void getChildrenMarkParent(int currentNode, int bfsId, bfsModel bfsItem) { List <verticeModel> children = getChildren(bfsId, currentNode); verticeModel smallestDist = new verticeModel(); smallestDist.Value = int.MaxValue; for (int childrenId = 0; childrenId < children.Count; childrenId++) { distances[children[childrenId].Destination - 1] = distances[currentNode] + children[childrenId].Value; if ((smallestDist == null || smallestDist.Value > children[childrenId].Value) && !visitedNodes[children[childrenId].Destination - 1]) { smallestDist = children[childrenId]; } } visitedNodes[currentNode] = true; currentNode = getNeighborOrChild(currentNode, bfsItem, visitedNodes, bfsId); if (currentNode != -1) { getChildrenMarkParent(currentNode, bfsId, bfsItem); } else { currentNode = smallestDist.Destination - 1; if (currentNode != -1) { getChildrenMarkParent(currentNode, bfsId, bfsItem); } } }
private int getNeighborOrChild(int currentNode, bfsModel bfsItem, bool[] visitedNodes, int bfsId) { List <verticeModel> rt = bfsItem.Vertices.FindAll(x => x.Destination == currentNode + 1 && visitedNodes[x.Source - 1]); verticeModel rtv = rt.Min(); if (rtv == null) { return(-1); } int sourceId = rtv.Source; List <verticeModel> children = getChildren(bfsId, sourceId - 1); int smallestDist = int.MaxValue; int returnNode = -1; for (int i = 0; i < children.Count; i++) { if (!visitedNodes[children[i].Destination - 1] && children[i].Value < smallestDist) { smallestDist = children[i].Value; returnNode = children[i].Destination - 1; } } return(returnNode); }
private void BtnAddVertices_Click(object sender, EventArgs e) { if (lbNodesAndEdgesForQueries.SelectedIndex > -1) { if (txtVerticesForNodesEdges.Text != "") { string selectedItemText = lbNodesAndEdgesForQueries.SelectedItem.ToString().Trim(); selectedItemText = selectedItemText.Substring(0, selectedItemText.IndexOf(": ")).Replace("Query number", "").Trim(); int selectedQueryId = Convert.ToInt32(selectedItemText); int bfsIdInList = bfsItems.FindIndex(x => x.QueryId == selectedQueryId); int verticeSource = Convert.ToInt32(txtVerticesForNodesEdges.Text.Split(' ')[0].Trim()); int verticeDestination = Convert.ToInt32(txtVerticesForNodesEdges.Text.Split(' ')[1].Trim()); verticeModel vm = new verticeModel(); vm.Source = verticeSource; vm.Destination = verticeDestination; vm.Value = Convert.ToInt32(txtVerticeValue.Text.Trim()); bfsItems[bfsIdInList].Vertices.Add(vm); lbVerticesNodesEdges.Items.Add(string.Format("Query number {0}: Vertices source {1} destination {2}", selectedQueryId, verticeSource, verticeDestination)); } else { MessageBox.Show("Please write the Vertice source and destination nodes"); } } else { MessageBox.Show("Select a Query from Node and Edge for Queries"); } }