public static IGraphWeight DeserializeWeight(byte[] bytes) { if (bytes != null) { XmlStream stream = new XmlStream("weight"); MemoryStream ms = new MemoryStream(bytes); stream.ReadStream(ms); GraphWeight weight = new GraphWeight(); weight.Load(stream); return(weight); } return(null); }
public void TraverseGraphForEdges(GraphWeight node, Dictionary <string, string> result, int sum) { if (node != null) { foreach (var edge in node.Edges) { if (!result.ContainsKey(edge.Node.Data)) { result.Add(edge.Node.Data, $"{(sum + edge.Weight)}"); } else { result[edge.Node.Data] += $", {(sum + edge.Weight)}"; } TraverseGraphForEdges(edge.Node, result, sum + edge.Weight); } } }
private void TraverseGraphForEdgesDFS(GraphWeight node, ref Dictionary <string, string> result) { if (node == null) { return; } if (result == null) { result = new Dictionary <string, string>(); } Stack <GraphWeight> qg = new Stack <GraphWeight>(); Stack <int> qt = new Stack <int>(); qg.Push(node); qt.Push(0); int nodeValue = 0; GraphWeight graphNode = null; while (qg.Count > 0) { graphNode = qg.Pop(); nodeValue = qt.Pop(); if (result.ContainsKey(graphNode.Data)) { result[graphNode.Data] += $",{nodeValue}"; } else { result.Add(graphNode.Data, $"{nodeValue}"); } foreach (Edge n in graphNode.Edges) { qg.Push(n.Node); qt.Push(nodeValue + n.Weight); } } }
private void TraverseGraphForEdgesBFS(GraphWeight node, ref Dictionary <string, string> result) { if (node == null) { return; } if (result == null) { result = new Dictionary <string, string>(); } Queue <GraphWeight> qg = new Queue <GraphWeight>(); Queue <int> qt = new Queue <int>(); qg.Enqueue(node); qt.Enqueue(0); int nodeValue = 0; GraphWeight graphNode = null; while (qg.Count > 0) { graphNode = qg.Dequeue(); nodeValue = qt.Dequeue(); if (result.ContainsKey(graphNode.Data)) { result[graphNode.Data] += $",{nodeValue}"; } else { result[graphNode.Data] = $"{nodeValue}"; } foreach (Edge n in graphNode.Edges) { qg.Enqueue(n.Node); qt.Enqueue(nodeValue + n.Weight); } } }
private void btnAddWeight_Click(object sender, EventArgs e) { GraphWeight weight = new GraphWeight("Weight " + (++_counter).ToString(), GraphWeightDataType.Double); lstWeights.Items.Add(new WeightListViewItem(weight)); }
private void btn_Given_a_weighted_graph_print_the_path_of_the_each_node_with_weight_Click(object sender, EventArgs e) { /* * A * / | \ * / | \ * 1 2 3 * / | \ * B C D * / | * 8 4 * / | * G----- E * / \ \ | * 7 10 \ 5 * / \ 6 | * H M \ | \-F */ GraphWeight A = new GraphWeight() { Data = "A" }; GraphWeight B = new GraphWeight() { Data = "B" }; GraphWeight C = new GraphWeight() { Data = "C" }; GraphWeight D = new GraphWeight() { Data = "D" }; GraphWeight E = new GraphWeight() { Data = "E" }; GraphWeight F = new GraphWeight() { Data = "F" }; GraphWeight G = new GraphWeight() { Data = "G" }; GraphWeight H = new GraphWeight() { Data = "H" }; GraphWeight M = new GraphWeight() { Data = "M" }; A.Edges.Add(new Edge() { Node = B, Weight = 1 }); A.Edges.Add(new Edge() { Node = C, Weight = 2 }); A.Edges.Add(new Edge() { Node = D, Weight = 3 }); B.Edges.Add(new Edge() { Node = G, Weight = 8 }); C.Edges.Add(new Edge() { Node = E, Weight = 4 }); E.Edges.Add(new Edge() { Node = F, Weight = 5 }); F.Edges.Add(new Edge() { Node = G, Weight = 6 }); G.Edges.Add(new Edge() { Node = H, Weight = 7 }); G.Edges.Add(new Edge() { Node = M, Weight = 10 }); Dictionary <string, string> nodeWeight = new Dictionary <string, string>(); StringBuilder result = new StringBuilder(); nodeWeight.Add(A.Data, "0"); TraverseGraphForEdges(A, nodeWeight, 0); result.AppendLine("Depth First Traversal"); foreach (string key in nodeWeight.Keys) { result.AppendLine($"{key} \t: {nodeWeight[key]}"); } nodeWeight = new Dictionary <string, string>(); this.TraverseGraphForEdgesBFS(A, ref nodeWeight); result.AppendLine(); result.AppendLine("Breath First Traversal"); foreach (string key in nodeWeight.Keys) { result.AppendLine($"{key} \t: {nodeWeight[key]}"); } nodeWeight = new Dictionary <string, string>(); this.TraverseGraphForEdgesDFS(A, ref nodeWeight); result.AppendLine(); result.AppendLine("Depth First Traversal"); foreach (string key in nodeWeight.Keys) { result.AppendLine($"{key} \t: {nodeWeight[key]}"); } MessageBox.Show($"Node path is \n {result.ToString()}"); }