From f273f46f556bdd3e643af0ad17f2b208b5379276 Mon Sep 17 00:00:00 2001 From: Nickylakhisarani <75842322+Nickylakhisarani@users.noreply.github.com> Date: Sun, 23 Oct 2022 09:36:24 +0530 Subject: [PATCH] Add files via upload --- graph/graph/bfs.cpp | 50 +++++ graph/graph/bfsinc.cpp | 44 +++++ graph/graph/bfst.cpp | 65 ++++++ graph/graph/bfstraversal.cpp | 72 +++++++ graph/graph/demo.cpp | 108 ++++++++++ graph/graph/dfs.cpp | 51 +++++ graph/graph/dfsusingadjacencymatrix.cpp | 62 ++++++ graph/graph/implementationofbfs.cpp | 65 ++++++ graph/graph/implementationofdfs.cpp | 39 ++++ graph/graph/implementationofgraph.cpp | 251 ++++++++++++++++++++++++ graph/graph/implementbfs2.cpp | 75 +++++++ graph/graph/practicebst.cpp | 188 ++++++++++++++++++ graph/graph/primsalgorithm.cpp | 63 ++++++ 13 files changed, 1133 insertions(+) create mode 100644 graph/graph/bfs.cpp create mode 100644 graph/graph/bfsinc.cpp create mode 100644 graph/graph/bfst.cpp create mode 100644 graph/graph/bfstraversal.cpp create mode 100644 graph/graph/demo.cpp create mode 100644 graph/graph/dfs.cpp create mode 100644 graph/graph/dfsusingadjacencymatrix.cpp create mode 100644 graph/graph/implementationofbfs.cpp create mode 100644 graph/graph/implementationofdfs.cpp create mode 100644 graph/graph/implementationofgraph.cpp create mode 100644 graph/graph/implementbfs2.cpp create mode 100644 graph/graph/practicebst.cpp create mode 100644 graph/graph/primsalgorithm.cpp diff --git a/graph/graph/bfs.cpp b/graph/graph/bfs.cpp new file mode 100644 index 0000000..3e44a72 --- /dev/null +++ b/graph/graph/bfs.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; + + +void bfs(int s,vector adj[],bool vis[],int n) +{ + queue q; + q.push(s); + + vis[s]=true; + while(!q.empty()) + { + int curr=q.front(); + q.pop(); + cout<>n,e; + + vector adj[n]; + bool vis[n]={false}; + for(int i=0;i>u>>v; + adj[u].push_back(v); + + } + + bfs(0,adj,vis,n); +} + + diff --git a/graph/graph/bfsinc.cpp b/graph/graph/bfsinc.cpp new file mode 100644 index 0000000..70f12f1 --- /dev/null +++ b/graph/graph/bfsinc.cpp @@ -0,0 +1,44 @@ +#include + + + + +void bfs(int g[][7],int start,int n) +{ + int i=start; + struct queue q; + int visited[7]={0}; + + print("%d",i); + visited[i]=1; + + enqueue(i); + while(!isEmpty()) + { + i=dequeue(); + for(int j=1;j +using namespace std; + +void printbfs(int** edges,int n,int sv) +{ + queue q; + bool *visited=new bool[n]; + for(int i=0;i>n; + cout<<"\n enter the number of edges \n"; + cin>>e; + + int** edges=new int*[n]; + for(int i=0;i>f; + cout<<"\n enter second vertex \n"<>s; + edges[f][s]=1; + edges[s][f]=1; + + } + bool *visited=new bool[n]; + for(int i=0;i +#include +#include +struct node{ + int data; + struct node* next; +}; + +struct node* front=NULL; +struct node* rear=NULL; + +void enqueue(int x){ + struct node* newnode=(struct node*)malloc(sizeof(node)); + newnode->data=x; + newnode->next=NULL; + if(front==NULL && rear==NULL) + { + front=newnode; + rear=newnode; + + } + else{ + rear->next=newnode; + rear=newnode; + } + + +} + +void display() +{ + struct node* temp; + if(front==NULL && rear==NULL) + { + printf("\n queue is empty\n"); + + } + else{ + temp=front; + while(temp!=NULL) + { + printf("%d",temp->data); + temp=temp->next; + } + } +} + +void dequeue(){ + struct node* temp=front; + if(front==NULL && rear==NULL) + { + printf("\n queue underflow\n"); + + } + else{ + printf("%d",front->data); + front=front->next; + free(temp); + } +} + +int peek(){ + if(front==NULL) + { + return INT_MAX; + } + else{ + return front->data; + } +} + + \ No newline at end of file diff --git a/graph/graph/demo.cpp b/graph/graph/demo.cpp new file mode 100644 index 0000000..7085b1b --- /dev/null +++ b/graph/graph/demo.cpp @@ -0,0 +1,108 @@ +#ifndef Queue_h +#define Queue_h +#include +struct Node +{ + int data; + struct Node *next; + +}*front=NULL,*rear=NULL; +void enqueue(int x) +{ + struct Node *t; + t=(struct Node*)malloc(sizeof(struct Node)); + if(t==NULL) + printf("Queue is FUll\n"); + else + { + t->data=x; + t->next=NULL; + if(front==NULL) + front=rear=t; + else + { + rear->next=t; + rear=t; + } + } + +} +int dequeue() +{ + int x=-1; + struct Node* t; + + if(front==NULL) + printf("Queue is Empty\n"); + else + { + x=front->data; + t=front; + front=front->next; + free(t); + } + return x; +} +int isEmpty() +{ + return front==NULL; +} +#endif /* Queue_h */ +#include +//#include "Queue.h" +void BFS(int G[][7],int start,int n) +{ + int i=start,j; + + int visited[7]={0}; + + printf("%d ",i); + visited[i]=1; + enqueue(i); + + while(!isEmpty()) + { + i=dequeue(); + for(j=1;j +using namespace std; + +class graph{ + public: + map visited; + map> adj; + + void addedge(int v,int w); + void dfs(int v); + +}; + +void graph::addedge(int v,int w) +{ + adj[v].push_back(w); +} +void graph::dfs(int v) +{ + visited[v]=true; + cout<::iterator i; + for(i=adj[v].begin();i!=adj[v].end();i++) + { + if(!visited[*i]) + dfs(*i); + } +} + + + + +int main() +{ + // Create a graph given in the above diagram + graph g; + g.addedge(0, 1); + g.addedge(0, 2); + g.addedge(1, 2); + g.addedge(2, 0); + g.addedge(2, 3); + g.addedge(3, 3); + + cout << "Following is Depth First Traversal" + " (starting from vertex 2) \n"; + g.dfs(2); + + return 0; +} \ No newline at end of file diff --git a/graph/graph/dfsusingadjacencymatrix.cpp b/graph/graph/dfsusingadjacencymatrix.cpp new file mode 100644 index 0000000..926928b --- /dev/null +++ b/graph/graph/dfsusingadjacencymatrix.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; + +void print(int **edges,int n,int sv,bool *visited) +{ + cout<>n; + cout<<"\n enter the number of edges \n"; + cin>>e; + + int** edges=new int*[n]; + for(int i=0;i>f; + cout<<"\n enter second vertex \n"<>s; + edges[f][s]=1; + edges[s][f]=1; + + } + bool *visited=new bool[n]; + for(int i=0;i +#include +#include +#include +#include +using namespace std; + +template + +class graph{ + //here we are creating a non weighted graph so we are ausing list + //value in the map is key + map > l; + + public: + void addedge(int x,int y) + //we want a bidirectional graph so we insert u node after that we insert v node or we push_back v node after it + { + l[x].push_back(y); + l[y].push_back(x); + + } + + void bfs(T srcn) + { + map visited; + queue q; + + q.push(srcn); + visited[srcn]=true; + + while(!q.empty()) + { + T node = q.front(); + q.pop(); + cout< g; + g.addedge(0,1); + g.addedge(1,2); + g.addedge(2,3); + g.addedge(3,4); + g.addedge(4,5); + + + g.bfs(0); + +} \ No newline at end of file diff --git a/graph/graph/implementationofdfs.cpp b/graph/graph/implementationofdfs.cpp new file mode 100644 index 0000000..2efa343 --- /dev/null +++ b/graph/graph/implementationofdfs.cpp @@ -0,0 +1,39 @@ +#include +#include +using namespace std; + +void dfs(int s,vector g[],bool *vis) +{ + + vis[s]=true; + cout<>n>>e; + + vector g[n]; + bool vis[n]; + + for(int i=0;i>e;i++) + { + int u,v; + cin>>u>>v; + g[u].push_back(v); + g[v].push_back(u); + } + + dfs(0,g,vis); + + cout< +using namespace std; + +class edge{ + public: + int destinationvertexid; + int weight; + edge() + { + + } + edge(int destvid,int w) + { + destinationvertexid=destvid; + weight=w; + } + void setedgevalue(int destvid,int w) + { + destinationvertexid=destvid; + weight=w; + } + void setWeight(int w) { + weight = w; + } + + int getdestinationvertexid() { + return destinationvertexid; + } + int getweight() { + return weight; + } + + +}; + +class vertex{ + public: + int stateid; + string statename; + list edgelist; + + vertex() + { + stateid=0; + statename=""; + } + vertex(int id,string name) + { + stateid=id; + statename=name; + } + int getstateid() + { + return stateid; + } + string getstatename() + { + return statename; + + } + void setstateid(int ids) + { + stateid=ids; + } + void setstatename(string names) + { + statename=names; + } + + list getedgelist() + { + return edgelist; + } + +}; + +class graph{ + public: + vector vertices; + //storing object of type vertex + + + void addvertex(vertex newvertex) + { + bool check = checkifvertexexistbyid(newvertex.getstateid()); + if(check==true) + { + cout<<"vertex with this id already exist \n"; + } + else{ + vertices.push_back(newvertex); + cout<<"\n new vertex added successfully \n "; + } + + } + + + bool checkifvertexexistbyid(int vid) + { + + for(int i=0;i> option; + vertex v1; + + switch (option) { + case 0: + + break; + + case 1: + cout << "Add Vertex Operation -" << endl; + cout << "Enter State ID :"; + cin >> id1; + cout << "Enter State Name :"; + cin >> sname; + v1.setstateid(id1); + v1.setstatename(sname); + g.addvertex(v1); + + + break; + + /* case 2: + cout << "Update Vertex Operation -" << endl; + cout << "Enter State ID of Vertex(State) to update :"; + cin >> id1; + cout << "Enter State Name :"; + cin >> sname; + g.updateVertex(id1, sname); + + break; + + case 3: + cout << "Delete Vertex Operation -" << endl; + cout << "Enter ID of Vertex(State) to Delete : "; + cin >> id1; + g.deleteVertexByID(id1); + + break; + + case 4: + cout << "Add Edge Operation -" << endl; + cout << "Enter ID of Source Vertex(State): "; + cin >> id1; + cout << "Enter ID of Destination Vertex(State): "; + cin >> id2; + cout << "Enter Weight of Edge: "; + cin >> w; + g.addEdgeByID(id1, id2, w); + + break; + + case 5: + cout << "Update Edge Operation -" << endl; + cout << "Enter ID of Source Vertex(State): "; + cin >> id1; + cout << "Enter ID of Destination Vertex(State): "; + cin >> id2; + cout << "Enter UPDATED Weight of Edge: "; + cin >> w; + g.updateEdgeByID(id1, id2, w); + + break; + + case 6: + cout << "Delete Edge Operation -" << endl; + cout << "Enter ID of Source Vertex(State): "; + cin >> id1; + cout << "Enter ID of Destination Vertex(State): "; + cin >> id2; + g.deleteEdgeByID(id1, id2); + + break; + + case 7: + cout << "Check if 2 Vertices are Neigbors -" << endl; + cout << "Enter ID of Source Vertex(State): "; + cin >> id1; + cout << "Enter ID of Destination Vertex(State): "; + cin >> id2; + check = g.checkIfEdgeExistByID(id1, id2); + if (check == true) { + cout << "Vertices are Neigbors (Edge exist)"; + } else { + cout << "Vertices are NOT Neigbors (Edge does NOT exist)"; + } + + break; + + case 8: + cout << "Print All Neigbors of a Vertex -" << endl; + cout << "Enter ID of Vertex(State) to fetch all Neigbors : "; + cin >> id1; + g.getAllNeigborsByID(id1); + + break; + + case 9: + cout << "Print Graph Operation -" << endl; + g.printGraph(); + + break; +*/ + default: + cout << "Enter Proper Option number " << endl; + } + cout << endl; + + +} while (option != 0); + + +} diff --git a/graph/graph/implementbfs2.cpp b/graph/graph/implementbfs2.cpp new file mode 100644 index 0000000..4f75019 --- /dev/null +++ b/graph/graph/implementbfs2.cpp @@ -0,0 +1,75 @@ +#include +using namespace std; + +class graph{ + int v; + list *adj; + + public: + graph(int v); + void addedge(int v,int w); + void bfs(int s); +}; + +graph::graph(int v) +{ + this->v=v; + adj = new list[v]; +} + +void graph::addedge(int v,int w) +{ + adj[v].push_back(w); +} + +void graph::bfs(int s) +{ + bool *visited=new bool[v]; + for(int i=0;i queue; + visited[s]=true; + queue.push_back(s); + + list::iterator i; + + while(!queue.empty()) + { + s=queue.front(); + cout< +using namespace std; + +class treenode{ + public: + int value; + treenode* left; + treenode* right; + treenode(){ + value=0; + left=NULL; + right=NULL; + } + treenode(int v){ + value=v; + left=NULL; + right=NULL; + } + +}; + +class bst{ + public: + treenode *root; + bst(){ + root=NULL; + } + bool isempty() + { + if(root==NULL) + { + return true; + } + else{ + return false; + } + } +}; + +void insertnode(treenode* newnode){ + if(root==NULL) + { + root=newnode; + cout<<"\n value inserted as root \n"; + } + else{ + treenode *temp=root; + while(temp!=NULL) + { + if(newnode->value==temp->value) + { + cout<<"value already exist "<valuevalue ) && (temp->left==NULL)) + { + temp->left=newnode; + cout<<"value inserted to left \n"<valuevalue) + { + temp=temp->left; + } + else if((newnode->value>temp->value) && (temp->right==NULL)) + { + temp->right=newnode; + cout<<"\n value inserted at right \n"<value>temp->value) + { + temp=temp->right; + } + + } + } +} + + +void preorder(treenode *root) +{ + if(root==NULL) + return; + cout<value; + preorder(root->left); + preorder(root->right); +} +void inorder(treenode *root) +{ + if(root==NULL) + return; + + inorder(root->left); + cout<value; + inorder(root->right); +} +void postorder(treenode *root) +{ + if(root==NULL) + return; + postorder(root->left); + postorder(root->right); + cout<value; +} +treenode *minvalue(treenode *node) +{ + treenode *current=node; + while(current->left!=NULL) + { + current->left=current; + } + return current; + +} +treenode* deletenode(treenode* root,int v) +{ + if(root==NULL) + { + return NULL; + } + else if(vvalue) + { + root->left=deletenode(root->left,v); + } + else if(v>root->value) + root->right=deletenode(root->right,v); + + else{ + if(root->left==NULL) + { + treenode *temp=root->right; + delete root; + return temp; + } + else if(root->right==NULL) + { + treenode *temp=root->left; + delete root; + return temp; + + } + else{ + treenode *temp=minvalue(root->right); + root->value=temp->value; + + root->right=deletenode(root->right,temp->value); + } + } +} + + + +int main() +{ + int option; + bst obj; + do{ + cout<<"what operation do you want to perform enter your choice here "<>option; + + } + treenode *newnode = new treenode(); + + switch(option) + { + case 0; + break; + + case 1: + cout<<"insert"<>val; + newnode->value-val; + obj.insertnode(newnode); + break; + + default: + cout<<"enter other option \n"< +using namespace std; +#define v 5 +int minkey(int dis[],bool mstset[]) +{ + int minimum=INT_MAX; + int vertex; + for(int v=0;v