Posts

Showing posts from June, 2023

DAA P8 graph coloring

  Graph coloring // A C++ program to implement greedy algorithm for graph coloring #include < iostream > #include < list > using namespace std ; // A class that represents an undirected graph class Graph {   int V ; // No. of vertices   list <int> * adj ; // A dynamic array of adjacency lists public :   // Constructor and destructor   Graph ( int V ) { this -> V = V ; adj = new list <int> [ V ]; }   ~Graph ()   { delete [] adj ; }   // function to add an edge to graph   void addEdge ( int v , int w );   // Prints greedy coloring of the vertices   void greedyColoring (); }; void Graph :: addEdge ( int v , int w ) {   adj [ v ]. push_back ( w );   adj [ w ]. push_back ( v ); // Note: the graph is undirected } // Assigns colors (starting from 0) to all vertices and prints // the assignment of colors void Graph :: greedyColoring () {   int result [ V ];   //...

DAA P7 Queens

 8 Queens #include < iostream > using namespace std ; #define N 8 void printBoard ( int board [ N ][ N ]) {     for ( int i = 0 ; i < N ; i ++ ) {       for ( int j = 0 ; j < N ; j ++ )          cout << board [ i ][ j ] << " " ;          cout << endl ;     } } bool isValid ( int board [ N ][ N ], int row , int col ) {     for ( int i = 0 ; i < col ; i ++ ) //check whether there is queen in the left or not       if ( board [ row ][ i ])           return false ;     for ( int i = row , j = col ; i >= 0 && j >= 0 ; i -- , j -- )       if ( board [ i ][ j ]) //check whether there is queen in the left upper diagonal or not           return false ;     for ( int i = row , j = col ; j >= 0 ...

DAA P6 flyod warshall

  Floyd Warshal // C++ Program for Floyd Warshall Algorithm #include < bits/stdc++.h > using namespace std ; // Number of vertices in the graph #define V 4 /* Define Infinite as a large enough value.This value will be used for vertices not connected to each other */ #define INF 99999 // A function to print the solution matrix void printSolution ( int dist [][ V ]); // Solves the all-pairs shortest path // problem using Floyd Warshall algorithm void floydWarshall ( int dist [][ V ]) {   int i , j , k ;   /* Add all vertices one by one to   the set of intermediate vertices.   ---> Before start of an iteration,   we have shortest distances between all   pairs of vertices such that the   shortest distances consider only the   vertices in set {0, 1, 2, .. k-1} as   intermediate vertices.   ----> After the end of an iteration,   vertex no. k is added to the set of   intermediate vertices and the set bec...

DAA P5 kruskal

  Kruskal // C++ program for Kruskal's algorithm to find Minimum // Spanning Tree of a given connected, undirected and // weighted graph #include < bits/stdc++.h > using namespace std ; // Creating shortcut for an integer pair typedef pair <int , int> iPair ; // Structure to represent a graph struct Graph {   int V , E ;   vector < pair <int , iPair > > edges ;   // Constructor   Graph ( int V , int E )   {     this -> V = V ;     this -> E = E ;   }   // Utility function to add an edge   void addEdge ( int u , int v , int w )   {     edges . push_back ( {w , {u , v}} );   }   // Function to find MST using Kruskal's   // MST algorithm   int kruskalMST (); }; // To represent Disjoint Sets struct DisjointSets {   int * parent , * rnk ;   int n ;   // Constructor.   DisjointSets ( int n )   {   ...

DAA P4 bfs, dfs

BFS #include < bits/stdc++.h > using namespace std ; class Graph {   int V ;   vector < list <int> > adj ; public :   Graph ( int V );   void addEdge ( int v , int w );   void BFS ( int s ); }; Graph :: Graph ( int V ) {   this -> V = V ;   adj . resize ( V ); } void Graph :: addEdge ( int v , int w ) {   adj [ v ] . push_back ( w ); } void Graph :: BFS ( int s ) {   vector <bool> visited ;   visited . resize ( V , false );   list <int> queue ;   visited [ s ] = true ;   queue . push_back ( s );   while ( ! queue . empty ()) {         s = queue . front ();     cout << s << " " ;     queue . pop_front ();     for ( auto adjacent : adj [ s ] ) {       if ( ! visited [ adjacent ] ) {         visited [ adjacent ] = true ;         q...

DAA P3 merge sort, quick sort

  Merge Sort #include < bits/stdc++.h > using namespace std ; void merge ( int array [], int const left , int const mid ,     int const right ) {   int const subArrayOne = mid - left + 1 ;   int const subArrayTwo = right - mid ;   auto * leftArray = new int [ subArrayOne ],     * rightArray = new int [ subArrayTwo ];   for ( auto i = 0 ; i < subArrayOne ; i ++ )     leftArray [ i ] = array [ left + i ];   for ( auto j = 0 ; j < subArrayTwo ; j ++ )     rightArray [ j ] = array [ mid + 1 + j ];   auto indexOfSubArrayOne = 0 , indexOfSubArrayTwo = 0 ;   int indexOfMergedArray = left ;   while ( indexOfSubArrayOne < subArrayOne     && indexOfSubArrayTwo < subArrayTwo ) {     if ( leftArray [ indexOfSubArrayOne ]       <= rightArray [ indexOfSubArrayTwo ]) {       ...