#practicing writing code without an editor because they don't let us use one for lab ijbol
Explore tagged Tumblr posts
mazhaipilipiri · 1 year ago
Text
include <iostream>
using namespace std;
class node{
public:
node* next;
int val;
node(int k){
val=k;
next=NULL;
}
class q{
public:
node* head;
node* tail;
q(){
head=NULL;
tail=NULL;
}
push(int k){
if (q->head==NULL){
q->head=k;
q->tail=k;
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
class graph{
int v;
vector<vector<int>> a;
graph(int vertex):v(vertex),a(vertex){}
void insert_edge(int u,int v){
a[u].push_back(v);
a[v].push_back(u);
}
void dfs(int s,vector<bool>& vis){
vector<bool> visited(v,false);
vector<int> st;
visited[s]=true;
st.push_back(s);
while(!st.empty()){
int c=st.back();
st.pop_back();
cout<<c<<" ";
for(int n:a[c]){
if(!visited[n]){
visited[n]=true;
st.push_back(n);
}
}
}
}
void bfs(int s){
vector<bool> visited(v,false);
queue<int> q;
visited[s]=true;
q.push(s);
while(!q.empty()){
int c=q.front();
q.pop();
cout<<c<<" ";
for(int n:a[c]){
if(!visited[n]){
visited[n]=true;
q.push(n);
}
}
}
}
bool connected(){
vector<bool> visited(v,false);
dfs(0,visited);
for(bool vi:visited){
if(!v){
return false;
}
}
return true;
}
bool directed(){
for(int i=0;i<v;i++){
for(int j:a[i]){
if(find(a[j].begin(),a[j].end(),i)==a[j].end()){
return true;
}
}
}
return false;
}
};
0 notes