public void dfsrecommend(string s) { int aktif = this.searchIdxNode(s); node node_aktif = this.nodes[aktif]; //copy adj graph adj_aktif = new graph(); foreach (node i in node_aktif.adjacent) { adj_aktif.addNode(i); } graph friend_recommend = new graph(); foreach (node i in this.nodes) { node temp = new node(i.vertex); if (temp.vertex != node_aktif.vertex && (!adj_aktif.contain(temp.vertex))) { friend_recommend.addNode(temp); } foreach (node j in i.adjacent) { int num = adj_aktif.searchIdxNode(j.vertex); if (num != -1) { temp.addAdj(adj_aktif.nodes[num]); } } } friend_recommend.AllInfo(); }
public void bfsRekurs(int idxTrav, graph nodesOnDepth, graph travelledNodes, int dist, List <int> distList) { if (idxTrav == travelledNodes.nodes.Count || dist == 0) { } else { foreach (node i in travelledNodes.nodes[idxTrav].adjacent) { if (travelledNodes.contain(i.vertex)) { // Nothing } else if (distList[idxTrav] + 1 < dist) { travelledNodes.addNode(i); distList.Add(distList[idxTrav] + 1); } else { nodesOnDepth.addNode(i); } } bfsRekurs(idxTrav + 1, nodesOnDepth, travelledNodes, dist, distList); } }
public void bfsRekurs(int idxTrav, node tujuan, graph travelledNodes, graph parentsGraph) { if (idxTrav == travelledNodes.nodes.Count || travelledNodes.contain(tujuan.vertex)) { } else { foreach (node i in travelledNodes.nodes[idxTrav].adjacent) { if (travelledNodes.contain(tujuan.vertex)) { break; } else if (travelledNodes.contain(i.vertex)) { // Nothing } else { travelledNodes.addNode(i); parentsGraph.nodes.Add(travelledNodes.nodes[idxTrav]); } } bfsRekurs(idxTrav + 1, tujuan, travelledNodes, parentsGraph); } }
public void friendRecommendation(string nodename, string method, RichTextBox rtb) { graph friendRec = new graph(); friendRec = this.getAllNodesWithMutualAdj(nodename, method); rtb.AppendText("Daftar rekomendasi teman untuk akun " + nodename + ":\n"); for (int i = 0; i < friendRec.nodes.Count; i++) { rtb.AppendText("Nama akun: " + friendRec.nodes[i].vertex + "\n"); rtb.AppendText(friendRec.nodes[i].countMutualAdjacent(this.nodes[searchIdxNode(nodename)]) + " mutual friend"); if (friendRec.nodes[i].countMutualAdjacent(this.nodes[searchIdxNode(nodename)]) > 1) { rtb.AppendText("s"); } rtb.AppendText(":\n"); List <node> mutualFriends = friendRec.nodes[i].getAllMutualAdjacent(this.nodes[searchIdxNode(nodename)]); foreach (node j in mutualFriends) { rtb.AppendText(j.vertex + "\n"); } rtb.AppendText("\n"); } if (friendRec.nodes.Count() == 0) { rtb.AppendText("Tidak ada yang cocok.\n"); } }
public graph bfs(string vertexawal, string vertextujuan, graph strippedGraph) { node awal = this.nodes[searchIdxNode(vertexawal)]; node tujuan = this.nodes[searchIdxNode(vertextujuan)]; graph travelledNodes = new graph(); graph parentsGraph = new graph(); int idxTrav; if (awal.adjCount() != 0) // Kalau tidak trigger, return list node isi node awal saja { travelledNodes.addNode(awal); parentsGraph.nodes.Add(this.nodes[searchIdxNode(vertexawal)]); idxTrav = 0; bfsRekurs(idxTrav, tujuan, travelledNodes, parentsGraph); int i = travelledNodes.nodes.Count - 1; while (i > 0) { strippedGraph.nodes.Insert(0, travelledNodes.nodes[i]); i = travelledNodes.searchIdxNode(parentsGraph.nodes[i].vertex); } } strippedGraph.nodes.Insert(0, travelledNodes.nodes[0]); return(travelledNodes); }
public graph getAllNodesWithMutualAdj(string nodeName, string method) { // METHOD INI SUDAH TERMASUK SORTING. // Cari node dengan nama nodeName node TheNode = this.nodes[searchIdxNode(nodeName)]; // Graf yang akan direturn. graph nodesWithMutualAdjacent = new graph(); if (method == "BFS") { nodesWithMutualAdjacent = this.bfs(nodeName, 2); } // DFS DIPISAH DI DFSREC // Sort for (int i = 0; i < nodesWithMutualAdjacent.nodes.Count; i++) { int maxIdx = i; for (int j = i + 1; j < nodesWithMutualAdjacent.nodes.Count; j++) { if (nodesWithMutualAdjacent.nodes[maxIdx].countMutualAdjacent(TheNode) < nodesWithMutualAdjacent.nodes[j].countMutualAdjacent(TheNode)) { maxIdx = j; } } node swap = nodesWithMutualAdjacent.nodes[i]; nodesWithMutualAdjacent.nodes[i] = nodesWithMutualAdjacent.nodes[maxIdx]; nodesWithMutualAdjacent.nodes[maxIdx] = swap; } return(nodesWithMutualAdjacent); }
public void dfsrecommend(string s, RichTextBox rtb) { graph g = new graph(); foreach (node i in this.nodes) { g.addNode(i); } int aktif = g.searchIdxNode(s); node node_aktif = g.nodes[aktif]; //copy adj graph adj_aktif = new graph(); foreach (node i in node_aktif.adjacent) { adj_aktif.addNode(i); } graph friend_recommend = new graph(); foreach (node i in g.nodes) { node temp = new node(i.vertex); if (temp.vertex != node_aktif.vertex && (!adj_aktif.contain(temp.vertex))) { friend_recommend.addNode(temp); } foreach (node j in i.adjacent) { int num = adj_aktif.searchIdxNode(j.vertex); if (num != -1) { temp.addAdj(adj_aktif.nodes[num]); } } } rtb.AppendText("Daftar rekomendasi teman untuk akun " + s + ":\n"); Boolean HasRecom = false; foreach (node i in friend_recommend.nodes) { if (i.adjCount() > 0) { HasRecom = true; } } if (HasRecom) { friend_recommend.sortGraphDescAdjCount(); friend_recommend.AllInfo(rtb); } else { rtb.AppendText("Tidak ada yang cocok.\n"); } }
public bool AllAdjVisited(graph g) { foreach (node i in this.adjacent) { if (!g.contain(i.vertex)) { return(false); } } return(true); }
public graph dfs(string awal, string tujuan) { flag.ketemu = false; node node_awal = this.nodes[searchIdxNode(awal)]; node node_tujuan = this.nodes[searchIdxNode(tujuan)]; graph travelled_node = new graph(); graph result = new graph(); travelled_node.addNode(node_awal); dfs_rek(node_awal, node_tujuan, travelled_node, result); return(result); }
public void dfs_rek(node aktif, node tujuan, graph trav, graph result) { if (aktif == tujuan) { flag.ketemu = true; foreach (node i in trav.nodes) { result.nodes.Add(i); } } else if (flag.ketemu == false) { if (aktif.adjCount() == 0 || aktif.AllAdjVisited(trav)) { Console.WriteLine("Dead End"); } else { if (!trav.contain(tujuan.vertex)) { foreach (node i in aktif.adjacent) { if (!flag.ketemu) { graph travelled = new graph(); foreach (node j in trav.nodes) { travelled.addNode(j); } if (!travelled.contain(i.vertex)) { travelled.addNode(i); dfs_rek(i, tujuan, travelled, result); if (travelled.contain(tujuan.vertex)) { return; } } } } } } } }
public graph bfs(string vertexawal, int dist) { node awal = this.nodes[searchIdxNode(vertexawal)]; graph nodesOnDepth = new graph(); int idxTrav = 0; if (awal.adjCount() != 0) // Kalau tidak trigger, return list node isi node awal saja { graph travelledNodes = new graph(); travelledNodes.addNode(awal); List <int> distList = new List <int>(); distList.Add(0); bfsRekurs(idxTrav, nodesOnDepth, travelledNodes, dist, distList); } return(nodesOnDepth); }
public graph bfs(string vertexawal, string vertextujuan) { node awal = this.nodes[searchIdxNode(vertexawal)]; node tujuan = this.nodes[searchIdxNode(vertextujuan)]; graph travelledNodes = new graph(); // graph parentGraph = new graph(); int idxTrav; if (awal.adjCount() != 0) // Kalau tidak trigger, return list node isi node awal saja { travelledNodes.addNode(awal); // parentGraph.Add(awal); idxTrav = 0; bfsRekurs(idxTrav, tujuan, travelledNodes); } return(travelledNodes); }
public void friendRecommendation(string nodename, string method) { graph friendRec = new graph(); friendRec = this.getAllNodesWithMutualAdj(nodename, method); Console.WriteLine("Daftar rekomendasi teman untuk akun {0}:", nodename); for (int i = 0; i < friendRec.nodes.Count; i++) { Console.WriteLine("Nama akun: {0}", friendRec.nodes[i].vertex); Console.WriteLine("{0} mutual friends:", friendRec.nodes[i].countMutualAdjacent(this.nodes[searchIdxNode(nodename)])); List <node> mutualFriends = friendRec.nodes[i].getAllMutualAdjacent(this.nodes[searchIdxNode(nodename)]); foreach (node j in mutualFriends) { Console.WriteLine("{0}", j.vertex); } Console.WriteLine(); } }
public void friendExplore(string namaNodeAwal, string namaNodeTujuan, string method) { graph ExploreResult = new graph(); if (method == "BFS") { graph dummy = this.bfs(namaNodeAwal, namaNodeTujuan, ExploreResult); } else if (method == "DFS") { ExploreResult = this.dfs(namaNodeAwal, namaNodeTujuan); } Console.WriteLine("Nama akun: {0} dan {1}", namaNodeAwal, namaNodeTujuan); if (ExploreResult.nodes.Count <= 2) { Console.WriteLine("Tidak ada jalur koneksi yang tersedia"); Console.WriteLine("Anda harus memulai koneksi baru itu sendiri."); } else { Console.Write("{0}", ExploreResult.nodes.Count - 2); if (ExploreResult.nodes.Count - 2 == 1) { Console.Write("st"); } else if (ExploreResult.nodes.Count - 2 == 2) { Console.Write("nd"); } else if (ExploreResult.nodes.Count - 2 == 3) { Console.Write("rd"); } else { Console.Write("th"); } Console.Write("-degree connection\n"); ExploreResult.AllVertexWithArrow(); } }
public void friendExplore(string namaNodeAwal, string namaNodeTujuan, string method, RichTextBox rtb) { graph ExploreResult = new graph(); if (method == "BFS") { graph dummy = this.bfs(namaNodeAwal, namaNodeTujuan, ExploreResult); } else if (method == "DFS") { ExploreResult = this.dfs(namaNodeAwal, namaNodeTujuan); } rtb.AppendText("Nama akun: " + namaNodeAwal + " dan " + namaNodeTujuan + "\n"); if (!ExploreResult.contain(namaNodeTujuan)) { rtb.AppendText("Tidak ada jalur koneksi yang tersedia\n"); rtb.AppendText("Anda harus memulai koneksi baru itu sendiri.\n"); } else { rtb.AppendText((ExploreResult.nodes.Count - 2).ToString()); if (ExploreResult.nodes.Count - 2 == 1) { rtb.AppendText("st"); } else if (ExploreResult.nodes.Count - 2 == 2) { rtb.AppendText("nd"); } else if (ExploreResult.nodes.Count - 2 == 3) { rtb.AppendText("rd"); } else { rtb.AppendText("th"); } rtb.AppendText("-degree connection\n"); ExploreResult.AllVertexWithArrow(rtb); } }
static void Main(string[] args) { graph a = new graph(); // Untuk Read File string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Jeremy\Documents\Backup Jeremy 5 Sep 20\Kuliah\Semester 4\Strategi Algoritma\Tubes2\Tubes2STIMA\src\tes.txt"); foreach (string line in lines) { string[] y = line.Split(" "); if (!a.contain(y[0])) { node temp = new node(y[0]); a.addNode(temp); if (!a.contain(y[1])) { node temp1 = new node(y[1]); a.addNode(temp1); temp.addAdj(temp1); } else { foreach (node i in a.nodes) { if (i.vertex == y[1]) { temp.addAdj(i); } } } } else { for (int i = 0; i < a.nodes.Count; i++) { if (a.nodes[i].vertex == y[0]) { if (!a.contain(y[1])) { node temp1 = new node(y[1]); a.addNode(temp1); a.nodes[i].addAdj(temp1); } else { foreach (node j in a.nodes) { if (j.vertex == y[1]) { a.nodes[i].addAdj(j); } } } } } } } // a.AllInfo(); // Console.Write("This is the nodes in graph.\n"); // a.AllVertex(); // string vawal = "G"; // string vtujuan = "H"; // graph x = a.bfs(vawal,vtujuan); // Console.Write("This is the BFS from {0} to {1}\n" ,vawal,vtujuan); // x.AllVertex(); // int dist = 3; // Console.Write("This is the BFS from {0} to {1} node away from it\n",vawal,dist); // graph b = a.bfs("G", dist); // b.AllVertex(); // graph empty = new graph(); // graph w = a; // w.friendRecommendation("G","BFS"); // w.friendExplore("A","G","BFS"); a.nodes.Sort((x, y) => x.vertex.CompareTo(y.vertex)); foreach (node i in a.nodes) { i.adjacent.Sort((x, y) => x.vertex.CompareTo(y.vertex)); } a.AllVertexWithArrow(); graph g = a.dfs("A", "G"); g.AllVertexWithArrow(); //graph g = a.dfs("A","G"); //udah ke sort semua, jadi ngga bingung tentang alphabetical order dalam search }