class Node: def __init__(self, value = None, pointer = None): self.value = value self.pointer = pointer | |
class LinkedQueue: | |
def __init__(self): | |
self.head = None | |
self.tail = None | |
def isEmpty(self): | |
return not bool(self.head) | |
def dequeue(self): | |
if self.head: | |
value = self.head.value | |
self.head = self.head.pointer | |
return value | |
def enqueue(self, value): | |
node = Node(value) | |
if not self.head: | |
self.head = node | |
self.tail = node | |
else: | |
if self.tail: | |
self.tail.pointer = node | |
self.tail = node | |
def size(self): | |
node = self.head | |
num_nodes = 0 | |
while node: | |
num_nodes += 1 | |
node = node.pointer | |
return num_nodes | |
def peek(self): | |
return self.head.value | |
def _print(self): | |
node = self.head | |
while node: | |
print(node.value,end=' ') | |
node = node.pointer | |
print() | |
if __name__ == '__main__': | |
queue = LinkedQueue() | |
print("Is the queue empty? ", queue.isEmpty()) | |
print("Adding 1 to 10 in queue element ") | |
for i in range(1,11): | |
queue.enqueue(i) | |
print("Queue size : ", queue.size()) | |
print("Queue front data : ", queue.peek()) | |
print("Remove queue front element :", queue.dequeue()) | |
print("Queue new front data : ", queue.peek()) | |
print("Is the queue empty? ", queue.isEmpty()) | |
print("Printing the queue element :",end=' ') | |
queue._print() | |
queue.enqueue(44) | |
queue.enqueue(99) | |
print("Printing the new queue element :",end=' ') | |
queue._print() | |
print("Queue size : ", queue.size()) | |
print("Queue front data : ", queue.peek()) |
Saturday, September 14, 2019
Queue implementation uisng linked list in python3
Queue implement using two stack in python3
Q.Implement queue using two Stack class Queue: def __init__(self): | |
self.in_stack = [] | |
self.out_stack = [] | |
def enqueue(self, data): | |
while self.out_stack: | |
self.in_stack.append(self.out_stack.pop()) | |
self.in_stack.append(data) | |
while self.in_stack: | |
self.out_stack.append(self.in_stack.pop()) | |
def dequeue(self): | |
if self.out_stack: | |
self.out_stack.pop() | |
def size(self): | |
return len(self.out_stack) | |
def frontData(self): | |
if self.out_stack: | |
return self.out_stack[-1] | |
def __repr__(self): | |
if self.out_stack: | |
return '{}'.format(self.out_stack) | |
def isEmpty(self): | |
return not (bool(self.out_stack)) | |
if __name__ == '__main__': | |
queue = Queue() | |
print("Is the queue empty? ", queue.isEmpty()) | |
print("Adding 1 to 10 in queue element ") | |
for i in range(1,11): | |
queue.enqueue(i) | |
print("Queue size : ", queue.size()) | |
print("Queue front data : ", queue.frontData()) | |
print("Remove queue front element ") | |
queue.dequeue() | |
print("Queue new front data : ", queue.frontData()) | |
print("Is the queue empty? ", queue.isEmpty()) | |
print("Printing the queue element : ") | |
print(queue) | |
queue.enqueue(44) | |
queue.enqueue(99) | |
print("Printing the new queue element : ") | |
print(queue) | |
print("Queue size : ", queue.size()) | |
print("Queue front data : ", queue.frontData()) |
Friday, March 2, 2018
Matt's Graph Book
--------------------------------------------------------------------------------------------------------
#Python Solution
--------------------------------------------------------------------------------------------------------
import resource
import sys
resource.setrlimit(resource.RLIMIT_STACK, [0x10000000, resource.RLIM_INFINITY])
sys.setrecursionlimit(0x10000001)
def dfs(u):
visited[u] = 1
for i in range(len(adj[u])):
v = adj[u][i]
if not visited[v]:
dfs(v)
n = int(input())
m = int(input())
adj = []
visited = [0]*(n+1)
for _ in range(n+1): adj.append([])
for i in range(m):
x,y = map(int,input().split())
adj[x].append(y)
adj[y].append(x)
d = int(input())
adj[d].clear()
dfs(1)
ans = 1
for i in range(n):
if not visited[i]:
ans = 0
break
print("Connected" if ans else "Not Connected")
--------------------------------------------------------------------------------------------------------
# C++ Solution
---------------------------------------------------------------------------------------------------------------------------------------------------
#include< bits/stdc++.h >
using namespace std;
vector< int >adj[1000001];
vector< bool >visited(1000001,false);
void dfs(int u)
{
visited[u] = true;
for(int i = 0; i < adj[u].size() ; i++){
int v = adj[u][i];
if(visited[v] == false)
dfs(v);
}
}
int main()
{
int n,m,x,y,d;
cin >> n >> m;
for(int i = 0; i < m ; i++){
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
cin >> d;
adj[d].clear();
dfs(1);
bool ans = true;
for(int i = 0; i < n ; i++){
if(visited[i] == false){
ans = false;
break;
}
}
cout << ((ans)? "Connected" : "Not Connected" ) << endl;
return 0;
}
#Python Solution
--------------------------------------------------------------------------------------------------------
import resource
import sys
resource.setrlimit(resource.RLIMIT_STACK, [0x10000000, resource.RLIM_INFINITY])
sys.setrecursionlimit(0x10000001)
def dfs(u):
visited[u] = 1
for i in range(len(adj[u])):
v = adj[u][i]
if not visited[v]:
dfs(v)
n = int(input())
m = int(input())
adj = []
visited = [0]*(n+1)
for _ in range(n+1): adj.append([])
for i in range(m):
x,y = map(int,input().split())
adj[x].append(y)
adj[y].append(x)
d = int(input())
adj[d].clear()
dfs(1)
ans = 1
for i in range(n):
if not visited[i]:
ans = 0
break
print("Connected" if ans else "Not Connected")
--------------------------------------------------------------------------------------------------------
# C++ Solution
---------------------------------------------------------------------------------------------------------------------------------------------------
#include< bits/stdc++.h >
using namespace std;
vector< int >adj[1000001];
vector< bool >visited(1000001,false);
void dfs(int u)
{
visited[u] = true;
for(int i = 0; i < adj[u].size() ; i++){
int v = adj[u][i];
if(visited[v] == false)
dfs(v);
}
}
int main()
{
int n,m,x,y,d;
cin >> n >> m;
for(int i = 0; i < m ; i++){
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
cin >> d;
adj[d].clear();
dfs(1);
bool ans = true;
for(int i = 0; i < n ; i++){
if(visited[i] == false){
ans = false;
break;
}
}
cout << ((ans)? "Connected" : "Not Connected" ) << endl;
return 0;
}
Thursday, March 1, 2018
Feasible relations
--------------------------------------------------------------------------------------------------------
#Python Solution
--------------------------------------------------------------------------------------------------------
import resource
import sys
resource.setrlimit(resource.RLIMIT_STACK, [0x10000000, resource.RLIM_INFINITY])
sys.setrecursionlimit(0x10000001)
def find(i):
if(subset[i] != i):
subset[i] = find(subset[i])
return subset[i]
def UnionSet(x,y):
xroot = find(x)
yroot = find(y)
subset[xroot] = yroot
t = int(input())
for _ in range(t):
n,m = map(int,input().split())
subset = [0]*(n+1)
for i in range(n):
subset[i] = i
ans = 0
for i in range(m):
x,op,y = input().split(' ')
x = int(x) - 1
y = int(y) - 1
if op == "=":
UnionSet(x,y)
elif find(x) == find(y):
ans = 1
print('YES' if not ans else 'NO')
--------------------------------------------------------------------------------------------------------
# C++ Solution
--------------------------------------------------------------------------------------------------------
#include< bits/stdc++.h >
using namespace std;
struct subset{
int parent ,rank ;
};
int find(struct subset subsets[],int i){
if(subsets[i].parent != i) subsets[i].parent = find(subsets,subsets[i].parent);
return subsets[i].parent;
}
void UnioSet(struct subset subsets[], int x, int y)
{
int xroot = find(subsets,x);
int yroot = find(subsets,y);
if(subsets[xroot].rank < subsets[yroot].rank)subsets[xroot].parent = yroot;
else if(subsets[xroot].rank > subsets[yroot].rank) subsets[yroot].parent = xroot;
else{
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}
int main()
{
int t,n,m;
cin >> t;
while(t--){
int x,y;
string op ;
cin >> n >> m;
struct subset* subsets = (struct subset*) malloc((n+1)* sizeof(struct subset));
for(int i = 0; i < n ; i++){
subsets[i].parent = i;
subsets[i].rank = 0;
}
int ans = 0;
for(int i = 0; i < m; i++){
cin >> x >> op >> y;
if(op == "=")
UnioSet(subsets,x,y);
else if(find(subsets,x) == find(subsets,y)) ans = 1;
}
cout << ((ans == 1)? "NO\n" : "YES\n");
}
return 0;
}
#Python Solution
--------------------------------------------------------------------------------------------------------
import resource
import sys
resource.setrlimit(resource.RLIMIT_STACK, [0x10000000, resource.RLIM_INFINITY])
sys.setrecursionlimit(0x10000001)
def find(i):
if(subset[i] != i):
subset[i] = find(subset[i])
return subset[i]
def UnionSet(x,y):
xroot = find(x)
yroot = find(y)
subset[xroot] = yroot
t = int(input())
for _ in range(t):
n,m = map(int,input().split())
subset = [0]*(n+1)
for i in range(n):
subset[i] = i
ans = 0
for i in range(m):
x,op,y = input().split(' ')
x = int(x) - 1
y = int(y) - 1
if op == "=":
UnionSet(x,y)
elif find(x) == find(y):
ans = 1
print('YES' if not ans else 'NO')
--------------------------------------------------------------------------------------------------------
# C++ Solution
--------------------------------------------------------------------------------------------------------
#include< bits/stdc++.h >
using namespace std;
struct subset{
int parent ,rank ;
};
int find(struct subset subsets[],int i){
if(subsets[i].parent != i) subsets[i].parent = find(subsets,subsets[i].parent);
return subsets[i].parent;
}
void UnioSet(struct subset subsets[], int x, int y)
{
int xroot = find(subsets,x);
int yroot = find(subsets,y);
if(subsets[xroot].rank < subsets[yroot].rank)subsets[xroot].parent = yroot;
else if(subsets[xroot].rank > subsets[yroot].rank) subsets[yroot].parent = xroot;
else{
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}
int main()
{
int t,n,m;
cin >> t;
while(t--){
int x,y;
string op ;
cin >> n >> m;
struct subset* subsets = (struct subset*) malloc((n+1)* sizeof(struct subset));
for(int i = 0; i < n ; i++){
subsets[i].parent = i;
subsets[i].rank = 0;
}
int ans = 0;
for(int i = 0; i < m; i++){
cin >> x >> op >> y;
if(op == "=")
UnioSet(subsets,x,y);
else if(find(subsets,x) == find(subsets,y)) ans = 1;
}
cout << ((ans == 1)? "NO\n" : "YES\n");
}
return 0;
}
Monk and Graph Problem
# Python Solution
import resource
import sys
sys.setrecursionlimit(0x100000)
#if you don't write this line you got runtime error ,coz python recursionlimit is soo poor , so you need upgrade recursionlimit
# Will segfault without this line.
#resource.setrlimit(resource.RLIMIT_STACK, [0x10000000, resource.RLIM_INFINITY])
def find_component(u):
global ans
if component[u] != -1:
return
component[u] = 0
for i in range(0,len(adj[u])):
ans += 1
find_component(adj[u][i])
n,m = map(int,input().split())
adj = []
component = [-1]*(100001)
for i in range(n+1): adj.append([])
for i in range(m):
x,y = map(int,input().split())
adj[x].append(y)
adj[y].append(x)
res = 0
for i in range(1,n+1):
ans = 0
if (component[i] == -1):
find_component(i)
res = max(res,ans//2)
print(res)
#C++ Solution
#include< bits/stdc++.h >
using namespace std;
vector< int >adj[100001];
vector< int >component(100001,-1);
int n,ans = 0;
void find_component(int u)
{
if (component[u] == 0) {
return ;
}
component[u] = 0;
for(int i = 0; i < adj[u].size() ; i++){
ans++;
find_component(adj[u][i]);
}
}
int main()
{
int m,x,y;
cin >> n >> m;
for(int i = 0; i < m; i++){
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
int components = 0,res = 0;
for(int i = 1; i <= n; i++){
ans = 0;
if(component[i] == -1){
find_component(i);
// component++ if need how many component graph are present
res = max(res,ans/2);
}
}
cout << res << endl;
return 0;
}
import resource
import sys
sys.setrecursionlimit(0x100000)
#if you don't write this line you got runtime error ,coz python recursionlimit is soo poor , so you need upgrade recursionlimit
# Will segfault without this line.
#resource.setrlimit(resource.RLIMIT_STACK, [0x10000000, resource.RLIM_INFINITY])
def find_component(u):
global ans
if component[u] != -1:
return
component[u] = 0
for i in range(0,len(adj[u])):
ans += 1
find_component(adj[u][i])
n,m = map(int,input().split())
adj = []
component = [-1]*(100001)
for i in range(n+1): adj.append([])
for i in range(m):
x,y = map(int,input().split())
adj[x].append(y)
adj[y].append(x)
res = 0
for i in range(1,n+1):
ans = 0
if (component[i] == -1):
find_component(i)
res = max(res,ans//2)
print(res)
#C++ Solution
#include< bits/stdc++.h >
using namespace std;
vector< int >adj[100001];
vector< int >component(100001,-1);
int n,ans = 0;
void find_component(int u)
{
if (component[u] == 0) {
return ;
}
component[u] = 0;
for(int i = 0; i < adj[u].size() ; i++){
ans++;
find_component(adj[u][i]);
}
}
int main()
{
int m,x,y;
cin >> n >> m;
for(int i = 0; i < m; i++){
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
int components = 0,res = 0;
for(int i = 1; i <= n; i++){
ans = 0;
if(component[i] == -1){
find_component(i);
// component++ if need how many component graph are present
res = max(res,ans/2);
}
}
cout << res << endl;
return 0;
}
Happy Vertices (Python3)
def dfs(u):
global ans
for i in range(len(adj[u])):
v = adj[u][i]
if not visited[v]:
visited[v] = len(adj[v])-1
if visited[v] > visited[u]:
ans += 1
dfs(v)
n,m = map(int,input().split())
ans = 0
adj = []
for i in range(n+1): adj.append([])
for i in range(m):
x,y = map(int,input().split())
adj[x].append(y)
adj[y].append(x)
visited = [0]*(n+1)
for i in range(1,n+1):
if not visited[i]:
visited[i] = len(adj[i])
dfs(i)
print(ans)
global ans
for i in range(len(adj[u])):
v = adj[u][i]
if not visited[v]:
visited[v] = len(adj[v])-1
if visited[v] > visited[u]:
ans += 1
dfs(v)
n,m = map(int,input().split())
ans = 0
adj = []
for i in range(n+1): adj.append([])
for i in range(m):
x,y = map(int,input().split())
adj[x].append(y)
adj[y].append(x)
visited = [0]*(n+1)
for i in range(1,n+1):
if not visited[i]:
visited[i] = len(adj[i])
dfs(i)
print(ans)
Prison Break (Python3)
n = 0
def recursive(u,v):
global ans
if(u == n-1 and v == n-1):
ans += 1
return
data[u][v] = 1
if(u-1 >= 0 and data[u-1][v] == 0):
recursive(u-1,v)
if(u+1 < n and data[u+1][v] == 0):
recursive(u+1,v)
if(v-1 >= 0 and data[u][v-1] == 0):
recursive(u,v-1)
if(v+1 < n and data[u][v+1] == 0):
recursive(u,v+1)
data[u][v] = 0
return
t = int(input())
for _ in range(t):
n = int(input())
data = []
for i in range(n):
data.append(list(map(int,input().split())))
ans = 0
recursive(0,0)
print(ans)
def recursive(u,v):
global ans
if(u == n-1 and v == n-1):
ans += 1
return
data[u][v] = 1
if(u-1 >= 0 and data[u-1][v] == 0):
recursive(u-1,v)
if(u+1 < n and data[u+1][v] == 0):
recursive(u+1,v)
if(v-1 >= 0 and data[u][v-1] == 0):
recursive(u,v-1)
if(v+1 < n and data[u][v+1] == 0):
recursive(u,v+1)
data[u][v] = 0
return
t = int(input())
for _ in range(t):
n = int(input())
data = []
for i in range(n):
data.append(list(map(int,input().split())))
ans = 0
recursive(0,0)
print(ans)
Prison Break
#include< bits/stdc++.h >
using namespace std;
int data[21][21];
int dfs(int u,int v,int n){
int ans = 0;
if( u == n and v == n) return 1;
data[u][v] = 1;
if(u - 1 >= 1 and data[u-1][v] == 0)
ans += dfs(u-1,v,n);
if(v - 1 >= 1 and data[u][v-1] == 0)
ans += dfs(u,v-1,n);
if(u+1 <= n and data[u+1][v] == 0)
ans += dfs(u+1,v,n);
if(v+1 <= n and data[u][v+1] == 0)
ans += dfs(u,v+1,n);
data[u][v] = 0;
return ans ;
}
int main()
{
freopen("in.txt","r",stdin);
int n, t;
cin >> t;
while(t--){
cin >> n;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
cin >> data[i][j];
if(data[1][1] == 1 and data[n][n] == 1) cout << 0 << endl;
else cout << dfs(1,1,n) << endl;
}
return 0;
}
using namespace std;
int data[21][21];
int dfs(int u,int v,int n){
int ans = 0;
if( u == n and v == n) return 1;
data[u][v] = 1;
if(u - 1 >= 1 and data[u-1][v] == 0)
ans += dfs(u-1,v,n);
if(v - 1 >= 1 and data[u][v-1] == 0)
ans += dfs(u,v-1,n);
if(u+1 <= n and data[u+1][v] == 0)
ans += dfs(u+1,v,n);
if(v+1 <= n and data[u][v+1] == 0)
ans += dfs(u,v+1,n);
data[u][v] = 0;
return ans ;
}
int main()
{
freopen("in.txt","r",stdin);
int n, t;
cin >> t;
while(t--){
cin >> n;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
cin >> data[i][j];
if(data[1][1] == 1 and data[n][n] == 1) cout << 0 << endl;
else cout << dfs(1,1,n) << endl;
}
return 0;
}
Wednesday, February 28, 2018
Happy Vertices
/* Name : Arif Khan
Date : 1/3/18
Problem Title: Happy Vertices
Problem Link : https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/practice-problems/algorithm/happy-vertices/
*/
#include< bits/stdc++.h >
using namespace std;
vector < int >adj[100001];
vector < int > visited(100001,0);
int ans = 0;
void dfs(int u){
for(int i = 0; i < adj[u].size() ; i++){
int v = adj[u][i];
if(visited[v] == 0){
visited[v] = adj[v].size() - 1;
if(visited[v] > visited[u]) ans++;
dfs(v);
}
}
}
int main()
{
int n , m , x , y;
cin >> n >> m;
for(int i = 1; i <= m ; i++){
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
for(int i = 1; i <= n; i++){
if(visited[i] == 0){
visited[i] = adj[i].size();
dfs(i);
}
}
cout << ans << endl;
return 0;
}
Date : 1/3/18
Problem Title: Happy Vertices
Problem Link : https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/practice-problems/algorithm/happy-vertices/
*/
#include< bits/stdc++.h >
using namespace std;
vector < int >adj[100001];
vector < int > visited(100001,0);
int ans = 0;
void dfs(int u){
for(int i = 0; i < adj[u].size() ; i++){
int v = adj[u][i];
if(visited[v] == 0){
visited[v] = adj[v].size() - 1;
if(visited[v] > visited[u]) ans++;
dfs(v);
}
}
}
int main()
{
int n , m , x , y;
cin >> n >> m;
for(int i = 1; i <= m ; i++){
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
for(int i = 1; i <= n; i++){
if(visited[i] == 0){
visited[i] = adj[i].size();
dfs(i);
}
}
cout << ans << endl;
return 0;
}
Bishu and his Girlfriend
#include < bits/stdc++.h >
using namespace std;
vector< int > adj[1001];
vector< bool >visited(1000,false);
int dsit[1001];
void dfs(int s)
{
memset(dsit,0,sizeof(dsit));
stack < int > st;
st.push(s);
dsit[s] = 0;
while(!st.empty()){
int u = st.top();
st.pop();
for(int i = 0; i < adj[u].size() ; i++){
int v = adj[u][i];
if(v > dsit[u]+1){
dsit[v] = dsit[u]+1;
st.push(v);
}
}
}
}
int main()
{
int n,m;
cin >> n;
for(int i = 0; i < n-1; i++){
int x,y;
cin >> x >> y;
// if undirected graph
adj[x].push_back(y);
adj[y].push_back(x); // if directed graph not needed this line
}
int res = INT_MAX,q,ans=0;
cin >> q;
int mal;
dfs(1);
for(int i = 0;i < q; i++){
cin >> mal;
if(dsit[mal] < res){
res = dsit[mal];
ans = mal;
}else if(res == dsit[mal]){
ans = min(ans,mal);
}
}
cout << ans << endl;
return 0;
}
using namespace std;
vector< int > adj[1001];
vector< bool >visited(1000,false);
int dsit[1001];
void dfs(int s)
{
memset(dsit,0,sizeof(dsit));
stack < int > st;
st.push(s);
dsit[s] = 0;
while(!st.empty()){
int u = st.top();
st.pop();
for(int i = 0; i < adj[u].size() ; i++){
int v = adj[u][i];
if(v > dsit[u]+1){
dsit[v] = dsit[u]+1;
st.push(v);
}
}
}
}
int main()
{
int n,m;
cin >> n;
for(int i = 0; i < n-1; i++){
int x,y;
cin >> x >> y;
// if undirected graph
adj[x].push_back(y);
adj[y].push_back(x); // if directed graph not needed this line
}
int res = INT_MAX,q,ans=0;
cin >> q;
int mal;
dfs(1);
for(int i = 0;i < q; i++){
cin >> mal;
if(dsit[mal] < res){
res = dsit[mal];
ans = mal;
}else if(res == dsit[mal]){
ans = min(ans,mal);
}
}
cout << ans << endl;
return 0;
}
Subscribe to:
Posts (Atom)