LISTING PROGRAM using System; using System.Collections.Generic; using System.ComponentModel; using System.Windows.Forms; using System.Linq; using System.Collections; using System.Diagnostics; namespace L_DequeGreedy {
public partial class MainForm : Form {
Stopwatch watch; string[] alphabet = { "a", //Pantai kahona "b", // Makam Papan Tinggi "c", // Makam Mahligai "d", // Pantai Binasi "e", // Pantai Barus "f", // Pulau Mursala "g", // Pulau Poncan "h", // Air Terjun Silaklak "i", // Tangga Seratus "j", // Batu Lubang "k", // Air Terjun Aloban "l", // Air Terjun Sihobuk
"m", // Air Terjun Labuan Sonang "n", // Pantai Hajoran
"o", // Gua Maria Mella "p", // Pantai Ujung Sibolga "q", // Pantai Pandan "r", // Pantai Bosur "s", // Pantai Batu Gajah "t" // Air terjun lubuk buaya };
Dictionary<string,Dictionary<string,double>> graph = new Dictionary<string, Dictionary<string, double>> {
{ "a",new Dictionary<string, double> { { "c", 12 }, { "b", 13.3 } } },
{ "b",new Dictionary<string, double> { { "c", 1.8 }, { "d", 24 }, { "e", 4.4 }, { "a", 13.3 } } }, { "c",new Dictionary<string, double> { { "e", 4.8 }, { "a", 12 }, { "b", 1.8 } } },
{ "d",new Dictionary<string, double> { { "e", 23.3 }, { "f", 27 }, { "b", 24 } } },
{ "e",new Dictionary<string, double> { { "f", 39 }, { "h", 50 }, { "b", 4.4 }, { "c", 4.8 }, { "d", 23.3 } } },
{ "f",new Dictionary<string, double> { { "g", 33 }, { "d", 27 }, { "e", 39 } } },
{ "g",new Dictionary<string, double> { { "i", 4 }, { "l", 6 }, { "m", 3 }, { "f", 33 } } },
{ "h",new Dictionary<string, double> { { "i", 16 }, { "k", 12 }, { "j", 14 }, { "o", 13.1 }, { "e", 50 } } }, { "i",new Dictionary<string, double> { { "g", 4 }, { "m", 5.4 }, { "l", 8 }, { "q", 9.7 }, { "o", 4.5 }, { " h", 16 }, { "p", 1 } } },
{ "j",new Dictionary<string, double> { { "k", 1.8 }, { "o", 13 }, { "h", 14 } } }, { "k",new Dictionary<string, double> { { "o", 12.2 }, { "h", 12 }, { "j", 1.8 } } },
{ "l",new Dictionary<string, double> { { "m", 6.4 }, { "q", 6.9 }, { "g", 6 }, { "i", 8 } } },
", 6.4 } } },
{ "n",new Dictionary<string, double> { { "s", 1.5 }, { "r", 3.5 }, { "t", 21 }, { "m", 14 }, { "q", 9 } } }, { "o",new Dictionary<string, double> { { "i", 4.5 }, { "h", 13.1 }, { "p", 4 }, { "j", 13 }, { "k", 12.2 } } },
{ "p",new Dictionary<string, double> { { "i", 1 }, { "q", 10.5 }, { "o", 4 } } },
{ "q",new Dictionary<string, double> { { "p", 10.5 }, { "n", 9 }, { "r", 1.5 }, { "i", 9.7 }, { "l", 6.9 }, { " m", 8 }, { "s", 5 } } },
{ "r",new Dictionary<string, double> { { "s", 3.4 }, { "m", 9.3 }, { "q", 1.5 }, { "n", 3.5 }, { "t", 24 } } }, { "s",new Dictionary<string, double> { { "q", 5 }, { "n", 1.5 }, { "t", 22 }, { "r", 3.4 } } },
{ "t",new Dictionary<string, double> { { "r", 24 }, { "s", 22 }, { "n", 21 } } }, }; public MainForm() { InitializeComponent(); AsalCombo.SelectedIndex = 0; TujuanCombo.SelectedIndex = 0; AlgoCombo.SelectedIndex = 0; watch = Stopwatch.StartNew(); watch.Stop(); }
public void LDeque(string mySource) {
const double Inf = 99999999; string source = mySource;
watch.Restart();
var d = new Dictionary<string, double>(); var p = new Dictionary<string, string>(); var q = new Deque();
foreach (var pair in graph) { string vertex = pair.Key; d.Add(vertex, Inf); p.Add(vertex, "None"); } d[source] = 0; q.AddBack(source); while (q.Length() != 0) { string u = (string)q.DeleteBack(); foreach (var pair in graph[u]) { string v = pair.Key; if (d[v] > d[u] + graph[u][v]) { d[v] = d[u] + graph[u][v]; p[v] = u; Debug.WriteLine("p[{0}] = {1}", v, u); if (!q.Contains(v)) q.AddFront(v); } } }
watch.Stop(); int num = 0; string nama;
dataGridView1.Rows.Clear();
foreach (var pair in d){
nama = AsalCombo.Items[num].ToString();
if (pair.Value.Equals(Inf))
dataGridView1.Rows.Insert(num, nama, getFullRoute(source, pair.Key, p), null);
else
dataGridView1.Rows.Insert(num, nama, getFullRoute(source, pair.Key, p), pair.Value); num++; } dataGridView1.Sort(jarak,ListSortDirection.Ascending); WaktuTB.Text = watch.Elapsed.TotalMilliseconds + " ms"; }
string getFullRoute(string asal, string tujuan, Dictionary<string, string> p){ string kembali = "Tidak Ada Rute!";
string a, b; b = tujuan; a = p[b];
if(!a.Equals("None"))
kembali = getLokasiWisata(a)+"-"+getLokasiWisata(b); while(!a.Equals("None")){
b = a; a = p[b];
if(!a.Equals("None"))
kembali = ""+getLokasiWisata(a)+"-"+getLokasiWisata(b) + " -> "+ kembali; } return kembali; }
string getLokasiWisata(string alpha){
if(alpha.Equals("a")) return AsalCombo.Items[0].ToString(); if(alpha.Equals("b")) return AsalCombo.Items[1].ToString(); if(alpha.Equals("c")) return AsalCombo.Items[2].ToString(); if(alpha.Equals("d")) return AsalCombo.Items[3].ToString(); if(alpha.Equals("e")) return AsalCombo.Items[4].ToString(); if(alpha.Equals("f")) return AsalCombo.Items[5].ToString(); if(alpha.Equals("g")) return AsalCombo.Items[6].ToString(); if(alpha.Equals("h")) return AsalCombo.Items[7].ToString(); if(alpha.Equals("i")) return AsalCombo.Items[8].ToString(); if(alpha.Equals("j")) return AsalCombo.Items[9].ToString(); if(alpha.Equals("k")) return AsalCombo.Items[10].ToString(); if(alpha.Equals("l")) return AsalCombo.Items[11].ToString(); if(alpha.Equals("m")) return AsalCombo.Items[12].ToString(); if(alpha.Equals("n")) return AsalCombo.Items[13].ToString();
if(alpha.Equals("o")) return AsalCombo.Items[14].ToString(); if(alpha.Equals("p")) return AsalCombo.Items[15].ToString(); if(alpha.Equals("q")) return AsalCombo.Items[16].ToString(); if(alpha.Equals("r")) return AsalCombo.Items[17].ToString(); if(alpha.Equals("s")) return AsalCombo.Items[18].ToString(); if(alpha.Equals("t")) return AsalCombo.Items[19].ToString(); return "Empty";
}
void ProsesButtonClick(object sender, EventArgs e) {
int SelectedAlgo = AlgoCombo.SelectedIndex; if (SelectedAlgo == 0) { LDeque(alphabet[AsalCombo.SelectedIndex]); } else { Greedy(alphabet[AsalCombo.SelectedIndex],alphabet[TujuanCombo.Selected Index]); } }
void Button1Click(object sender, EventArgs e) {
var frm = new ShowGraph(); frm.ShowDialog();
}
void Greedy(string source, string dest){ string simpan="";
dataGridView1.Rows.Clear(); watch.Restart();
var result = new List<string>(); result.Add(source);
simpan += getLokasiWisata(source); double totalJarak = 0;
string current_node = ""; double local_max = 0; const int num = 0; bool unreachable = false; while ((!result.Contains(dest))) { current_node = result[result.Count - 1];
local_max = graph[current_node].Values.Min(); foreach (var pair in graph[current_node]) { if ((pair.Value).Equals(local_max)) { if (result.Contains(pair.Key)) {
simpan = "No Route Reachable using Greedy in this Graph!"; unreachable = true; break; } result.Add(pair.Key); simpan += " -> " + getLokasiWisata(pair.Key); totalJarak += pair.Value; } } if(unreachable) break; }
if(unreachable)
dataGridView1.Rows.Insert(num, getLokasiWisata( dest), simpan, "Infinit"); else
dataGridView1.Rows.Insert(num, getLokasiWisata( dest), simpan, totalJarak);
WaktuTB.Text = watch.Elapsed.TotalMilliseconds + " ms"; }
void AlgoComboSelectedValueChanged(object sender, EventArgs e) {
int SelectedAlgo = AlgoCombo.SelectedIndex; if (SelectedAlgo == 0) { TujuanCombo.Enabled = false; } else { TujuanCombo.Enabled = true; } } }
public class Deque : ArrayList {
public void AddFront(object a) { Insert(0, a);
}
public void AddBack(object a) { Add(a);
}
public object DeleteFront() { object a = this[0];
RemoveAt(0); return a; }
public object DeleteBack() { int size = this.Count; object a = this[size - 1]; RemoveAt(size - 1); return a;
}
public object GetFront() { return this[0];
}
public object GetEnd() { int size = Count; return this[size - 1]; }
public bool IsEmpty() { if (Count == 0)
return true; return false; }
public int Length() { return Count; }
} }