Posts

Showing posts from December, 2020

Dijkstra Algorithm code (using Global variables)

 #include<bits/stdc++.h> #include<limits.h> using namespace std; #define V 9 void print_dijkstra(int dst[V]) {     int i;     cout<<"NODE\tdst\n";     for(i=0;i<V;i++)     cout<<i<<"\t"<<dst[i]<<endl; } int mindst(int dst[V], bool mstset[V]) {     int min=INT_MAX, min_index,i;     for(i=0;i<V;i++)     {         if(mstset[i]==false && dst[i]<min)         {             min=dst[i];             min_index=i;         }     }     return min_index; } void dijkstra(int graph[V][V]) {     int dst[V], i, edge,x,u;     bool mstset[V];     for(i=0;i<V;i++)     {         dst[i]=INT_MAX;         mstset[i]=false;     }     cout<...

UNDIRECTED GRAPHS (create, BFS, DFS, adjacent nodes)

 BFS uses Queue and DFS uses Stack for implementation. #include<bits/stdc++.h> using namespace std; int adj[15][15]; void dfs(int v, int n) {   //before Exploring the first node completely(push it to stack),    //move on to the exploration its of another connected node,    //if no node is connected or left further for exploration than pop from stack,   //and print to visit that node.   int stk[n],i,top=-1,visited[n+1]{0};   top++;   stk[top]=v;   cout<<"DFS is : "<<v<<" ";   visited[v]=1;   while(top>=0)   {       v=stk[top];       top-=1;       if(visited[v]==0)         {cout<<v<<" "; visited[v]=1;}         for(i=1;i<=n;i++)         if(adj[v][i]==1 && visited[i]==0)       {top++;stk[top]=i;}   } } void display(int n) {     int i,j; ...