//Memvisualisasikan graf hasil public static Microsoft.Msagl.Drawing.Graph colorizedGrafVisualization(Graf graf, List <Edge> rute) { int i; Microsoft.Msagl.Drawing.Graph grafVisualization = new Microsoft.Msagl.Drawing.Graph("graf"); int N = graf.getGraphNode().Count; Graf frontEndGraph = FrontEndUtility.deletedDuplicatedEdgesGraph(graf, graf.getGraphNode().Count); foreach (Node node in frontEndGraph.getGraphNode()) { if (node.getEdges().Count == 0) { grafVisualization.AddNode(node.getNamaNode()); } foreach (Edge edge in node.getEdges()) { double roundedBobot = Math.Round(edge.getBobot() * 100000, 2); var garis = grafVisualization.AddEdge(edge.getNode().getNamaNode(), roundedBobot.ToString(), edge.getNext().getNamaNode()); garis.Attr.ArrowheadAtTarget = Microsoft.Msagl.Drawing.ArrowStyle.None; garis.Attr.ArrowheadAtSource = Microsoft.Msagl.Drawing.ArrowStyle.None; for (i = 0; i < rute.Count; i++) { bool edgeFirstCheck = rute[i].getNode().getNamaNode() == edge.getNode().getNamaNode() && rute[i].getNext().getNamaNode() == edge.getNext().getNamaNode(); bool edgeSecondCheck = rute[i].getNode().getNamaNode() == edge.getNext().getNamaNode() && rute[i].getNext().getNamaNode() == edge.getNode().getNamaNode(); if (edgeFirstCheck || edgeSecondCheck) { garis.Attr.Color = Microsoft.Msagl.Drawing.Color.Blue; if (i == 0) { grafVisualization.FindNode(rute[i].getNode().getNamaNode()).Attr.FillColor = Microsoft.Msagl.Drawing.Color.LightBlue; grafVisualization.FindNode(rute[i].getNext().getNamaNode()).Attr.FillColor = Microsoft.Msagl.Drawing.Color.LightGreen; } else if (i == rute.Count - 1) { grafVisualization.FindNode(rute[i].getNode().getNamaNode()).Attr.FillColor = Microsoft.Msagl.Drawing.Color.Turquoise; grafVisualization.FindNode(rute[i].getNext().getNamaNode()).Attr.FillColor = Microsoft.Msagl.Drawing.Color.LightGreen; } else { grafVisualization.FindNode(rute[i].getNode().getNamaNode()).Attr.FillColor = Microsoft.Msagl.Drawing.Color.Turquoise; grafVisualization.FindNode(rute[i].getNext().getNamaNode()).Attr.FillColor = Microsoft.Msagl.Drawing.Color.Turquoise; } } } } } return(grafVisualization); }
//Memvisualisasikan graf awal public static Microsoft.Msagl.Drawing.Graph grafVisualization(Graf graf) { Microsoft.Msagl.Drawing.Graph grafVisualization = new Microsoft.Msagl.Drawing.Graph("graf"); int N = graf.getGraphNode().Count; //gViewer1.Controls.Clear(); Graf frontEndGraph = FrontEndUtility.deletedDuplicatedEdgesGraph(graf, graf.getGraphNode().Count); foreach (Node node in frontEndGraph.getGraphNode()) { if (node.getEdges().Count == 0) { grafVisualization.AddNode(node.getNamaNode()); } foreach (Edge edge in node.getEdges()) { double roundedBobot = Math.Round(edge.getBobot() * 100000, 2); var garis = grafVisualization.AddEdge(edge.getNode().getNamaNode(), roundedBobot.ToString(), edge.getNext().getNamaNode()); garis.Attr.ArrowheadAtTarget = Microsoft.Msagl.Drawing.ArrowStyle.None; garis.Attr.ArrowheadAtSource = Microsoft.Msagl.Drawing.ArrowStyle.None; } } return(grafVisualization); }
//submit private void button2_Click(object sender, EventArgs e) { if (comboBox2.Text != "awal" && comboBox3.Text != "tujuan") { label7.Text = ""; //label4.Text = "Jalur"; Microsoft.Msagl.Drawing.Graph grafVisualization = new Microsoft.Msagl.Drawing.Graph("graf"); int i, j; // input simpul awal dan tujuan string awal = comboBox2.Text; string tujuan = comboBox3.Text; graf = new Graf(input); Node nodeAwal = graf.searchNode(awal); Node nodeTujuan = graf.searchNode(tujuan); if (!graf.searchNode(nodeAwal) || !graf.searchNode(nodeTujuan)) { //Console.WriteLine("Simpul tidak ada"); } else { //ALGORITMA A* List <Edge> rute = new List <Edge>(); List <Node> ruteNode = new List <Node>(); Node nodeAwalAlgo = nodeAwal; j = 0; while (nodeAwalAlgo != nodeTujuan) //selama nodeAwalAlgo bukan nodeTujuan { ruteNode.Add(nodeAwalAlgo); Dictionary <Node, double> results = new Dictionary <Node, double>(); int jumlahEdges = nodeAwalAlgo.getEdges().Count; i = 0; j += 1; while (i < jumlahEdges) { //g(n) = distance dari nodeAwalAlgo ke n double sumG = 0; if (rute.Count > 0) { foreach (Edge edge in rute) { sumG += edge.getBobot(); } } double g = nodeAwalAlgo.getEdges()[i].getBobot() + sumG; //h(n) = straight line distance from n ke nodeTujuan double h = Edge.euclideanDistance(nodeAwalAlgo.getEdges()[i].getNext().getKoordinatNode(), nodeTujuan.getKoordinatNode()); //f(n) = g(n) + h(n) double f = g + h; if (!ruteNode.Contains(nodeAwalAlgo.getEdges()[i].getNext())) { results.Add(nodeAwalAlgo.getEdges()[i].getNext(), f); } i += 1; } Node tempNodeAwal = nodeAwalAlgo; if (results.Count > 0) { //ambil nilai f minimum, update nodeAwalAlgo nodeAwalAlgo = results.OrderBy(KeyValuePair => KeyValuePair.Value).First().Key; Edge edgeRute = new Edge(tempNodeAwal, nodeAwalAlgo); rute.Add(edgeRute); } else { break; } } //visualisasi graf hasil grafVisualization = FrontEndUtility.colorizedGrafVisualization(graf, rute); gViewer1.Graph = grafVisualization; //hitung jarak rute yang dihasilkan double sumBobot = 0; foreach (Edge edge in rute) { sumBobot += edge.getBobot() * 100000; } label7.Text = sumBobot.ToString() + " m"; //kasus tidak ada tujuan if (nodeAwalAlgo != nodeTujuan) { grafVisualization = FrontEndUtility.grafVisualization(graf); gViewer1.Graph = grafVisualization; label7.Text = "Tidak ada jalur"; } } } else { label4.Text = "Tidak ada jalur"; } }
//graf copy constructor public Graf(Graf G) { this.nodeGraf = G.nodeGraf; }