Advertisement
Guest User

Untitled

a guest
Nov 28th, 2013
8,893
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include<iostream>
  2. #define NODES 6
  3. #include<stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. // Nodes of the adjacency lists
  8. typedef struct node
  9. {
  10.     int name;
  11.     int weight;
  12.     struct node* next;
  13. }node;
  14.  
  15.  
  16. //Header of the adjacency lists
  17. typedef struct graph_node
  18. {
  19.     int name;
  20.     int visited;
  21.     int parent;
  22.     struct node* list;
  23. }graph_node;
  24.  
  25. void add_edge(int i,int w,int j,graph_node g[])
  26. {
  27.     node* temp = g[i].list;
  28.    
  29.     node* new_node = (node*) malloc(sizeof(node));
  30.     new_node->name = j;
  31.     new_node->weight = w;
  32.     new_node->next=NULL;
  33.     if( temp==NULL)
  34.     {
  35.         g[i].list = new_node;
  36.         return;
  37.     }
  38.     else
  39.     {
  40.         g[i].list = new_node;
  41.         new_node->next = temp;
  42.         return;
  43.     }
  44. }
  45.  
  46. // Used for noting the paths
  47. int vertex_list[100];
  48. int vertex_count = 0;
  49.  
  50.  
  51. void printList()
  52. {
  53.     for(int i=0;i<vertex_count;i++)
  54.     {
  55.         cout<<vertex_list[i]<<" -- ";
  56.     }
  57.     cout<<"\n";
  58. }
  59.  
  60. void findPaths(graph_node g[],int start)
  61. {
  62.     vertex_list[vertex_count++] = start;
  63.     g[start].visited = 1;
  64.     node* temp = g[start].list;
  65.     int flag = 0;
  66.     // Flag is used to check the dead end of the path
  67.     while(temp)
  68.     {
  69.         if(g[temp->name].visited == 0)
  70.         {
  71.             flag = 1;
  72.             findPaths(g,temp->name);
  73.         }
  74.         temp = temp->next;
  75.     }
  76.     if(flag == 0)
  77.     {
  78.         // If all are having visited == 1 , there is no new node, so its a dead end
  79.         printList();
  80.     }
  81.     g[start].visited = 0;
  82.     vertex_count--;
  83. }
  84.  
  85. void initGraph(graph_node g[])
  86. {
  87.     add_edge(0,4,1,g);
  88.     add_edge(1,1,2,g);
  89.     add_edge(1,4,3,g);
  90.     add_edge(2,2,4,g);
  91.     add_edge(0,2,5,g);
  92.     add_edge(5,2,3,g);
  93. }
  94.  
  95. void printGraph(graph_node g[])
  96. {
  97.     for(int i=0;i<NODES;i++)
  98.     {
  99.         cout<<"\t\t"<<g[i].name<<" ----> ";
  100.         node* temp = g[i].list;
  101.         while(temp)
  102.         {
  103.             cout<<temp->name<<" "<<" ";
  104.             temp = temp->next;
  105.         }
  106.         cout<<"\n";
  107.     }
  108. }
  109.  
  110.  
  111. int main()
  112. {
  113.     graph_node graph[NODES];
  114.    
  115.     for(int i=0;i<NODES;i++)
  116.     {
  117.         graph[i].name = i;
  118.         graph[i].list = NULL;
  119.         graph[i].visited = 0;
  120.     }
  121.     cout<<"THis is the graph adjacency list : \n";
  122.     initGraph(graph);
  123.     printGraph(graph);
  124.     cout<<"These are the following paths : \n";
  125.     findPaths(graph,0);
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement