Latihan Graph Menggunakan Adjacent Matrix dan Traversal DFS - Deep First Search Secara Recursive DFS
1: #include <iostream>
2: using namespace std;
3: int e = 7; //jumlah edge
4: int n = 5; //jumlah vertex/node
5: bool mat[5][5] = { false }; //matrix 2D untuk menyimpan graph
6: int result[5];
7: int idxResult = 0;
8: bool isInResult(int n) {
9: for (int i = 0; i < idxResult; i++) {
10: if (n == result[i])return true;
11: }
12: return false;
13: }
14: void addToResult(int n) {
15: result[idxResult] = n;
16: idxResult++;
17: }
18: void DFS(int parent) {
19: if (!isInResult(parent)) { //cek di Result sebelumnya
20: addToResult(parent);
21: for (int i = 0; i < n; i++) { //mencari childs from parent
22: if (mat[parent][i] == true) {
23: DFS(i); //memasukkan setiap child ke DFS
24: }
25: }
26: }
27: }
28: int main() { //Undirected Graph
29: int v1[] = { 1,1,2,2,2,3,4 }; //hubungan antar vertex
30: int v2[] = { 2,5,3,4,5,4,5 };
31: for (int i = 0; i < e; i++) { //memasukkan undirected graph kedalam matrix
32: mat[v1[i] - 1][v2[i] - 1] = true;
33: mat[v2[i] - 1][v1[i] - 1] = true;
34: }
35: for (int i = 0; i < n; i++) { //menampilkan matriks graph
36: for (int j = 0; j < n; j++) {
37: cout << mat[i][j] << " ";
38: }
39: cout << endl;
40: } cout << endl;
41: int firstVertex = 0; //inisialisasi vertex awal
42: DFS(firstVertex);
43: for (int i = 0; i < n; i++) {
44: cout << result[i] + 1 << " ";
45: } cout << endl;
46: system("pause");
47: }
Special thanks to Samsung Academy SRIN Lecturer:
1. Pak Risman Adnan Mattotorang
2. Mas Leonardi
3. Mas Gilang
4. Mas Susanto Tedja
1: #include <iostream>
2: using namespace std;
3: int e = 7; //jumlah edge
4: int n = 5; //jumlah vertex/node
5: bool mat[5][5] = { false }; //matrix 2D untuk menyimpan graph
6: int result[5];
7: int idxResult = 0;
8: bool isInResult(int n) {
9: for (int i = 0; i < idxResult; i++) {
10: if (n == result[i])return true;
11: }
12: return false;
13: }
14: void addToResult(int n) {
15: result[idxResult] = n;
16: idxResult++;
17: }
18: void DFS(int parent) {
19: if (!isInResult(parent)) { //cek di Result sebelumnya
20: addToResult(parent);
21: for (int i = 0; i < n; i++) { //mencari childs from parent
22: if (mat[parent][i] == true) {
23: DFS(i); //memasukkan setiap child ke DFS
24: }
25: }
26: }
27: }
28: int main() { //Undirected Graph
29: int v1[] = { 1,1,2,2,2,3,4 }; //hubungan antar vertex
30: int v2[] = { 2,5,3,4,5,4,5 };
31: for (int i = 0; i < e; i++) { //memasukkan undirected graph kedalam matrix
32: mat[v1[i] - 1][v2[i] - 1] = true;
33: mat[v2[i] - 1][v1[i] - 1] = true;
34: }
35: for (int i = 0; i < n; i++) { //menampilkan matriks graph
36: for (int j = 0; j < n; j++) {
37: cout << mat[i][j] << " ";
38: }
39: cout << endl;
40: } cout << endl;
41: int firstVertex = 0; //inisialisasi vertex awal
42: DFS(firstVertex);
43: for (int i = 0; i < n; i++) {
44: cout << result[i] + 1 << " ";
45: } cout << endl;
46: system("pause");
47: }
Special thanks to Samsung Academy SRIN Lecturer:
1. Pak Risman Adnan Mattotorang
2. Mas Leonardi
3. Mas Gilang
4. Mas Susanto Tedja