Advertisement
Infernale

5 - UCS

Aug 31st, 2019
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. #define MAX 1000
  6.  
  7. struct Node{
  8.     int n, weight;
  9. }node; //struct untuk deklarasi type 'node' sehingga dapat memiliki attribut suatu titik
  10.  
  11. int flag[MAX], previ[MAX], dist[MAX], vertex, source, destination;
  12. int graph[MAX][MAX];
  13.  
  14. std::vector <Node> vec_nodes;
  15.  
  16. void add(int N, int cost){
  17.     node.n = N;
  18.     node.weight = cost;
  19.     vec_nodes.push_back(node);
  20. }
  21.  
  22. bool data_sort(Node a, Node b){
  23.     return a.weight < b.weight;
  24. }
  25.  
  26. void UCS(){
  27.     for(int i = 1; i <= vertex; i++){ //Initiate value tiap penyimpanan
  28.         flag[i] = 0;
  29.         dist[i] = 10e9;
  30.         previ[i] = -1;
  31.     }
  32.     dist[source] = 0; //Value untuk source node (35 - 38)
  33.     previ[source] = -1;
  34.     add(source, 0);
  35.  
  36.     while(!vec_nodes.empty()){
  37.         int u = vec_nodes[0].n;
  38.         vec_nodes.erase(vec_nodes.begin());
  39.         for(int i = 1; i <= vertex; i++){
  40.             if(graph[u][i] != 0 && flag[i] == 0){ //Node u dan i terhubung langsung oleh 1 garis
  41.                 if(dist[u] + graph[u][i] < dist[i]){ //Kondisi untuk jika jaraknya lebih kecil maka kemungkinan akan menjadi hasil final
  42.                     dist[i] = dist[u] + graph[u][i];
  43.                     previ[i] = u;
  44.                 }
  45.                 add(i, dist[i]); //Add node i ke vector yang menyimpan jaraknya
  46.                 sort(vec_nodes.begin(), vec_nodes.end(), data_sort); //Vector diurutkan sesuai prinsip UCS yang mengambil node dengan jarak terpendek dari root
  47.             }
  48.         }
  49.         flag[u] = -1; //Tanda bahwa titik tsb sudah ditemukan
  50.     }
  51. }
  52.  
  53. void printPath(int destination){
  54.     if(destination == source)
  55.         printf("%d ", source);
  56.     else if(previ[destination] == -1)
  57.         return;
  58.     else{
  59.         printPath(previ[destination]);
  60.         printf("%d ", destination);
  61.     }
  62. }
  63.  
  64. void printDistance(){
  65.     int i;
  66.     printf("\nDistance Measurement:\n");
  67.     for(i = 1; i <= vertex; ++i){
  68.         printf("%d -> %d = %d \n", source, i, dist[i]);
  69.     }
  70.     printf("\n");
  71. }
  72.  
  73. int main(){
  74.     int edges, u, v, w;
  75.     printf("Enter number of edges: "); //Jumlah garis
  76.     scanf("%d", &edges);
  77.     vertex = -1;
  78.     printf("Enter %d edges (from-to-distance) each separated by a space:\n", edges);
  79.     for(int i = 0; i < edges; i++){ //Buat input data graf
  80.         scanf("%d %d %d", &u, &v, &w);
  81.         graph[u][v] = w;
  82.         vertex = std::max(u, std::max(v, vertex)); //Buat nyari jumlah vertex
  83.     }
  84.     printf("Enter source and destination: ");
  85.     scanf("%d %d", &source, &destination);
  86.     UCS(); //Algoritma utama Uniform-cost search
  87.     printDistance(); //Jarak terpendek source node ke semua titik
  88.  
  89.     printf("The path from %d to %d is given below : \n", source, destination);
  90.     printPath(destination); //Untuk print jalur source node ke target node
  91.  
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement