Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.38 KB | None | 0 0
  1. // Dijkstra'sshortest path algorithm.
  2. // The program is for adjacency matrix representation of the graph
  3. #include <stdio.h>
  4. #include <uart.h>
  5. #include <system/peripherals.h>
  6. #include <libacc.h>
  7.  
  8. // Number of vertices in the graph
  9. #define V 24
  10.  
  11. typedef char bool;
  12. #define true 1
  13. #define false 0
  14. #define INT_MAX  131071
  15.  
  16. int printSolution(int dist[], int n) {
  17.     printf("\r\nVertex   Distance from Source\r\n");
  18.     for (int i = 0; i < V; i++)
  19.        printf("%d \t\t %d\r\n", i, dist[i]);
  20. }
  21.  
  22. void dijkstra(int graph[V][V], int src) {
  23.     int min = 0, min_index = 0, u = 0;
  24.     // The output array.  dist[i] will hold the shortest distance from src to i
  25.     int dist[V];    
  26.     // sptSet[i] will true if vertex i is included in shortest
  27.     // path tree or shortest distance from src to i is finalized
  28.     bool sptSet[V];
  29.  
  30.     for (int i = 0; i < V; i++) {
  31.        //NO_SYNTHESIS;
  32.        dist[i] = INT_MAX, sptSet[i] = false;
  33.     }
  34.  
  35.     //printSolution(dist, V);
  36.     dist[src] = 0;
  37.  
  38.     // Find shortest path for all vertices
  39.     for (int count = 0; count < V-1; count++) {
  40.         // Pick the minimum distance vertex from the set of vertices not
  41.         // yet processed. u is always equal to src in first iteration.
  42.         min = INT_MAX;
  43.         for (int v = 0; v < V; v++) {
  44.             //NO_SYNTHESIS;
  45.             if (sptSet[v] == false && dist[v] <= min) {
  46.                 min = dist[v];
  47.                 min_index = v;
  48.             }
  49.         }
  50.         u = min_index;
  51.         sptSet[u] = true;
  52.  
  53.         /*
  54.          * Update dist value of the adjacent vertices of the picked vertex.
  55.          * Update dist[v] only if is not in sptSet, there is an edge from
  56.          * u to v, and total weight of path from src to  v through u is
  57.          * smaller than current value of dist[v]
  58.          */
  59.         for (int v = 0; v < V; v++) {
  60.             //NO_SYNTHESIS;
  61.             //printf("\r\n v: %d", v);
  62.             if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX
  63.                 //&& graph[u][v] < dist[v]) {
  64.                 && dist[u]+graph[u][v] < dist[v]) {
  65.                 dist[v] = dist[u] + graph[u][v];
  66.                 //dist[v] = graph[u][v];
  67.                 //printf("\r\n dist[v]: %d graph[u][v]: %d", dist[v],graph[u][v]);
  68.                 //printf("\r\n dist[u]: %d sptSet[v]: %d", dist[u],sptSet[v]);
  69.             }
  70.         }
  71.     }
  72.     printSolution(dist, V);
  73. }
  74.  
  75. int main() {
  76.     stdio_uart_open(UART_LIGHT_0);
  77.     TIMER_0->limit = 131072;    //2^17     = 0x20000
  78.     TIMER_1->limit = 262143;    //2^18 - 1 = 0x3ffff
  79.     /* Example Graph */
  80.    // int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0},
  81.    //                    {4, 0, 8, 0, 0, 0, 0, 11,0},
  82.    //                    {0, 8, 0, 7, 0, 4, 0, 0, 2},
  83.    //                    {0, 0, 7, 0, 9, 14,0, 0, 0},
  84.    //                    {0, 0, 0, 9, 0, 10,0, 0, 0},
  85.    //                    {0, 0, 4, 0, 10,0, 2, 0, 0},
  86.    //                    {0, 0, 0, 14,0, 2, 0, 1, 6},
  87.    //                    {8, 11,0, 0, 0, 0, 1, 0, 7},
  88.    //                    {0, 0, 2, 0, 0, 0, 6, 7, 0}
  89.    //                  };
  90.  
  91.  
  92.     int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0},
  93.                        {4, 0, 8, 0, 0, 0, 0, 11,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0},
  94.                        {0, 8, 0, 7, 0, 4, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0},
  95.                        {0, 0, 7, 0, 9, 14,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0},
  96.                        {0, 0, 0, 9, 0, 10,0, 0, 0, 0, 0, 6,15, 8, 0, 0, 0, 0,12, 0, 0, 0, 0 ,0},
  97.                        {0, 0, 4, 0, 10,0, 2, 0, 0, 0, 0,12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0},
  98.                        {0, 0, 0, 14,0, 2, 0, 1, 6, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 0, 0, 0, 0 ,0},
  99.                        {8, 11,0, 0, 0, 0, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0 ,0},
  100.                        {0, 0, 2, 0, 0, 0, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0},
  101.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13, 7, 0, 0, 0, 0, 0, 0, 0 ,0},
  102.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0},
  103.                        {0, 0, 0, 0, 6,12, 0, 0, 0, 0, 0, 0, 8, 0, 6, 0, 0, 0, 0, 0, 4, 0, 0 ,0},
  104.                        {0, 0, 0, 0,15, 0, 0, 0, 0, 0, 0, 8, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0},
  105.                        {0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 1, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0 ,0},
  106.                        {0, 0, 0, 0, 0, 0, 0, 0, 0,13, 0, 6, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,9},
  107.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0},
  108.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0 ,0},
  109.                        {0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 2, 0, 5, 0, 0 ,0},
  110.                        {0, 0, 0, 0,12, 0, 5, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 0, 0, 0 ,0},
  111.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0 ,0},
  112.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0 ,0},
  113.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ,0},
  114.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 ,2},
  115.                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 2 ,0}
  116.                      };
  117.  
  118.      printf("\r\n === Dijkstra Algorithm ===\r\n");
  119.  
  120.      // HW Execution
  121.      libacc_enable_accels();
  122.      TIMER_0->control |= TIMER_CNT_RST;
  123.      TIMER_0->control &= !TIMER_CNT_RST;
  124.      TIMER_1->control |= TIMER_CNT_RST;
  125.      TIMER_0->control &= !TIMER_CNT_RST;    
  126.      TIMER_1->control = TIMER_EN;
  127.      TIMER_0->control = TIMER_EN;
  128.      dijkstra(graph, 0);  
  129.      TIMER_0->control = 0;
  130.      TIMER_1->control = 0;
  131.      printf("\r\n HW timer value: %6u ov: %6u", TIMER_0->value, TIMER_1->value);
  132.      
  133.  
  134.      // SW Execution
  135.      libacc_disable_accels();
  136.      TIMER_0->control |= TIMER_CNT_RST;
  137.      TIMER_0->control &= !TIMER_CNT_RST;
  138.      TIMER_1->control |= TIMER_CNT_RST;
  139.      TIMER_0->control &= !TIMER_CNT_RST;  
  140.      TIMER_1->control = TIMER_EN;
  141.      TIMER_0->control = TIMER_EN;
  142.      dijkstra(graph, 0);  
  143.      TIMER_0->control = 0;
  144.      TIMER_1->control = 0;
  145.      printf("\r\n SW timer value: %6u ov: %6u", TIMER_0->value, TIMER_1->value);
  146.  
  147.      while(1);
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement