0% found this document useful (0 votes)
19 views22 pages

Practice

The document contains C++ code for implementing graphs and graph algorithms including: 1) Defining a graph class with adjacency map representation and functions to add edges and print adjacency lists 2) Functions for common graph algorithms like BFS, DFS, cycle detection, topological sorting, shortest paths 3) Implementing minimum spanning tree algorithms like Prim's and Kruskal's algorithm 4) Defining a max heap class and functions for heap operations

Uploaded by

sunsam098
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views22 pages

Practice

The document contains C++ code for implementing graphs and graph algorithms including: 1) Defining a graph class with adjacency map representation and functions to add edges and print adjacency lists 2) Functions for common graph algorithms like BFS, DFS, cycle detection, topological sorting, shortest paths 3) Implementing minimum spanning tree algorithms like Prim's and Kruskal's algorithm 4) Defining a max heap class and functions for heap operations

Uploaded by

sunsam098
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

#include<bits/stdc++.

h>

using namespace std;

class graph{

public:

map<int,set<int>>adj;

void addEdge(int u,int v,bool d){

adj[u].insert(v);

if(!d){

adj[v].insert(u);

void print_adj(){

for(auto i:adj){

cout<<i.first<<"-> ";

for(auto j:i.second){

cout<<j<<",";

cout<<endl;

};

void bfs(int start,map<int,set<int>>&adj,vector<bool>&vis){

queue<int>q;

q.push(start);

vis[start]=1;

while(!q.empty()){

int front = q.front();


q.pop();

cout<<front;

for(auto i:adj[front]){

if(!vis[i]){

q.push(i);

vis[i]=1;

void dfs(int start,map<int,set<int>>&adj,vector<bool>&vis){

vis[start]=1;

cout<<start;

for(auto i:adj[start]){

if(!vis[i]){

dfs(i,adj,vis);

bool cycle_bfs(int start,map<int,set<int>>&adj,vector<bool>&vis,vector<int>&parent){

queue<int>q;

q.push(start);

vis[start]=1;

parent[start]=-1;

while(!q.empty()){

int front = q.front();

q.pop();
for(auto i:adj[front]){

if(!vis[i]){

q.push(i);

vis[i]=1;

parent[i]=front;

else if(parent[front]!=i){

return true;

return false;

bool cycle_dfs(int start,map<int,set<int>>&adj,vector<bool>&vis,int parent){

vis[start]=1;

for(auto i:adj[start]){

if(!vis[i]){

bool is_cycle = cycle_dfs(i,adj,vis,start);

if(is_cycle){

return true;

else if(parent!=i){

return true;

return false;

}
bool directed_cycle_dfs(int start,map<int,set<int>>&adj,vector<bool>&vis,vector<bool>&dvis){

vis[start]=1;

dvis[start]=1;

for(auto i:adj[start]){

if(!vis[i]){

bool is_cycle = directed_cycle_dfs(i,adj,vis,dvis);

if(is_cycle){

return true;

else if(dvis[i]){

return true;

dvis[start]=0;

return false;

void topological_sort(int start,map<int,set<int>>&adj,vector<bool>&vis,stack<int>&ans){

vis[start]=1;

for(auto i:adj[start]){

if(!vis[i]){

topological_sort(i,adj,vis,ans);

ans.push(start);

void topo_bfs_kahns(map<int,set<int>>&adj,int n){


vector<int>ind(n+1);

for(auto i:adj){

for(auto j:i.second){

ind[j]++;

queue<int>q;

for(int i=1;i<=n;i++){

if(ind[i]==0){

q.push(i);

while(!q.empty()){

int front =q.front();

q.pop();

cout<<front;

for(auto i:adj[front]){

ind[i]--;

if(ind[i]==0){

q.push(i);

void shortest_edge_path(map<int,set<int>>&adj,int n,int s,int d){

vector<bool>vis(n+1);

vector<int>parent(n+1);

queue<int>q;
q.push(1);

vis[1]=1;

parent[1]=-1;

while(!q.empty()){

int front = q.front();

q.pop();

for(auto i:adj[front]){

if(!vis[i]){

q.push(i);

vis[i]=1;

parent[i]=front;

stack<int>ans;

int curr=d;

ans.push(curr);

while(curr!=s){

curr=parent[curr];

ans.push(curr);

while(!ans.empty()){

cout<<ans.top();

ans.pop();

int main(){
graph g;

int n,m;cin>>n>>m;

for(int i=0;i<m;i++){

int u,v;

cin>>u>>v;

g.addEdge(u,v,0);

g.print_adj();

vector<bool>vis(n+1);

vector<bool>dvis(n+1);

vector<int>parent(n+1);

// bfs(1,g.adj,vis);

// vector<bool>v(n);

// cout<<endl;

// dfs(1,g.adj,vis);

// cout<<cycle_bfs(1,g.adj,vis,parent);

// cout<<cycle_dfs(1,g.adj,vis,-1);

// cout<<directed_cycle_dfs(1,g.adj,vis,dvis);

// stack<int>topo;

// topological_sort(1,g.adj,vis,topo);

// while(!topo.empty()){

// cout<<topo.top();

// topo.pop();

// }

// topo_bfs_kahns(g.adj,n);

// shortest_edge_path(g.adj,n,1,8);

}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include<bits/stdc++.h>

using namespace std;

class graph{

public:

map<int,set<pair<int,int>>>adj;

void addEdge(int u,int v,int w,bool d){

adj[u].insert({v,w});

if(!d){

adj[v].insert({u,w});

void print_adj(){

for(auto i:adj){

cout<<i.first<<"-> ";

for(auto j:i.second){

cout<<'['<<j.first<<','<<j.second<<']'<<", ";

cout<<endl;

};

void topological_sort(int start,map<int,set<pair<int,int>>>&adj,vector<bool>&vis,stack<int>&ans){

vis[start]=1;

for(auto i:adj[start]){
if(!vis[i.first]){

topological_sort(i.first,adj,vis,ans);

ans.push(start);

void shortest_path_directed(map<int,set<pair<int,int>>>&adj,int so,int n){

stack<int>s;

vector<bool>vis(n);

topological_sort(0,adj,vis,s);

vector<int>dist(n,INT_MAX);

dist[so]=0;

while(!s.empty()){

int top=s.top();

s.pop();

if(dist[top]!=INT_MAX){

for(auto i:adj[top]){

if(dist[top]+i.second < dist[i.first]){

dist[i.first]=dist[top]+i.second;

for(auto i:dist){

cout<<i<<" ";

}
void short_dist_undirected(map<int,set<pair<int,int>>>&adj,int n,int src){

set<pair<int,int>>s;

vector<int>dist(n,INT_MAX);

dist[src]=0;

s.insert({0,src});

while(!s.empty()){

int top=s.begin()->second;

int dis=s.begin()->first;

s.erase(s.begin());

for(auto &i:adj[top]){

if(i.second+dis<dist[i.first]){

if(s.find({dist[i.first],i.first}) != s.end()){

s.erase(s.find({dist[i.first],i.first}));

dist[i.first]=i.second+dis;

s.insert({dist[i.first],i.first});

cout<<endl;

for(auto &i:dist){

cout<<i<<" ";

void prim_mst_graph(map<int,set<pair<int,int>>>&adj,int n,int src){

vector<int>key(n);

vector<bool>mst(n);
vector<int>parent(n);

for(int i=0;i<n;i++){

key[i]=INT_MAX;

mst[i]=0;

parent[i]=-1;

key[src]=0;

int mini;

int u;

for(int i=0;i<n;i++){

mini=INT_MAX;

for(int j=0;j<n;j++){

if(key[j]<mini && mst[j]==0){

mini=key[j];

u=j;

mst[u]=1;

for(auto & k:adj[u]){

if(mst[k.first]!=1 && k.second<key[k.first]){

key[k.first]=k.second;

parent[k.first]=u;

cout<<endl;

for(int i=0;i<n;i++){
cout<<parent[i]<<" ";

//////////////////////////////////////////////////////////////////////////////////////////////////////

bool cmp(vector<int> &a,vector<int>&b){

return a[2]<b[2];

void makeSet(vector<int>&parent,vector<int>&rank){

for(int i=0;i<parent.size();i++){

parent[i]=i;

rank[i]=0;

int findParent(int node,vector<int>&parent){

if(parent[node]==node){

return node;

return parent[node]=findParent(parent[node],parent);

void unionSet(int u,int v,vector<int>&parent,vector<int>&rank){

if(rank[u]<rank[v]){

parent[u]=v;

else if(rank[u]>rank[v]){
parent[v]=u;

else{

parent[v]=u;

rank[u]++;

void kruskal_mst_wt(){

int n,m;

cin>>n>>m;

vector<vector<int>>vec;

int u,v,w;

for(int i=0;i<m;i++){

cin>>u>>v>>w;

vec.push_back({u,v,w});

sort(vec.begin(),vec.end(),cmp);

vector<int>parent(n);

vector<int>rank(n);

makeSet(parent,rank);

int mW=0;

for(int i=0;i<m;i++){

int u=findParent(vec[i][0],parent);

int v=findParent(vec[i][1],parent);

int w=vec[i][2];

if(v!=u){

mW+=w;

unionSet(u,v,parent,rank);

}
}

cout<<mW;

int main(){

graph g;

int n,m;cin>>n>>m;

for(int i=0;i<m;i++){

int u,v,w;

cin>>u>>v>>w;

g.addEdge(u,v,w,0);

g.print_adj();

// shortest_path(g.adj,1,n);

// short_dist_undirected(g.adj,n,0);

// prim_mst_graph(g.adj,n,0);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include<bits/stdc++.h>

using namespace std;

class heap{

int arr[100];
public:

int size=0;

void push(int val){

size=size+1;

int index=size;

arr[index]=val;

while(index>1){

int parent =index/2;

if(arr[index]>arr[parent]){

swap(arr[index],arr[parent]);

index=parent;

else{

return;

void heapify(int i){

int largest=i;

int left=2*i;

int right=2*i+1;

if(left<=size && arr[left]>arr[largest]){

largest=left;

if(right<=size && arr[right]>arr[largest]){

largest=right;

if(largest!=i){

swap(arr[i],arr[largest]);
heapify(largest);

void pop(){

arr[1]=arr[size];

size--;

heapify(1);

int top(){

return arr[1];

bool is_empty(){

return size;

};

//////////////////////////////////////////////////////

void heapify1(int arr[],int i,int size){

int largest=i;

int left=2*i;

int right=2*i+1;

if(left<=size && arr[left]>arr[largest]){

largest=left;

if(right<=size && arr[right]>arr[largest]){

largest=right;

if(largest!=i){

swap(arr[i],arr[largest]);

heapify1(arr,largest,size);
}

void Hsort(int arr[],int n){

for(int i=n/2;i>0;i--){

heapify1(arr,i,n);

int size=n;

while(size>1){

swap(arr[size],arr[1]);

size--;

heapify1(arr,1,size);

for(int i=1;i<=n;i++){

cout<<arr[i]<<" ";

//////////////////////////////////////////////////////

int main(){

// heap h;

// h.push(9);

// h.push(4);

// h.push(21);

// h.push(15);

// cout<<h.top();

// h.pop();

// cout<<h.top();
// h.pop();

// cout<<h.top();

// cout<<h.is_empty();

int arr[]={0,3,1,4,5,2};

Hsort(arr,5);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include<bits/stdc++.h>

using namespace std;

struct node{

int key;

int val;

node* next;

node(){

key=-1;

val=-1;

next=NULL;

node(int k,int v){

key =k;

val = v;

next=NULL;

};
class HashTable{

node **arr;

int size;

public:

HashTable(int n){

arr = new node*[n];

for(int i=0;i<n;i++){

arr[i]=NULL;

size=n;

int hashFunc(int k){

return k%size;

void insert(int k,int v){

int hashIndex = hashFunc(k);

node* newNode = new node(k,v);

if(!arr[hashIndex]){

arr[hashIndex]= newNode;

else if(find(k)==INT_MIN){

newNode->next = arr[hashIndex];

arr[hashIndex]=newNode;

else{

newNode = arr[hashIndex];

while(newNode->key!=k){

newNode=newNode->next;
}

newNode->val=v;

int find(int k){

int hashIndex = hashFunc(k);

node* temp = arr[hashIndex];

while(temp && temp->key!=k){

temp = temp->next;

if(!temp){

return INT_MIN;

else{

return temp->val;

void del(int k){

int hashIndex = hashFunc(k);

node* temp = arr[hashIndex];

if(temp==NULL){

return;

if(temp->key == k){

arr[hashIndex]=NULL;

while(temp->next && temp->next->key!=k){

temp = temp->next;

}
if(temp->next){

temp->next=temp->next->next;

int SIZE(){

return size;

void printTable(){

for(int i=0;i<size;i++){

node* temp =arr[i];

while(temp!=NULL){

cout<<temp->key<<" -> "<<temp->val<<endl;

temp=temp->next;

};

int main(){

int n;

cin>>n;

int k,v;

HashTable m(n);

for(int i=0;i<n;i++){

cin>>k>>v;

m.insert(k,v);

m.printTable();
m.insert(234,96);

cout<<m.find(234);

m.printTable();

You might also like