Skip to content

Commit f92535e

Browse files
author
unknown
committed
graphs part 1
1 parent f5113b0 commit f92535e

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed

adjmatrix.py.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def printmatrix(matrix):
2+
r,c = len(matrix),len(matrix[0])
3+
for i in range(r):
4+
for j in range(c):
5+
print(matrix[i][j],end=" ")
6+
print()
7+
8+
v,e = map(int,input().split())
9+
matrix = [[0]*v for i in range(v)]
10+
'''
11+
for i in range(e):
12+
u,v = map(str,input().split())
13+
u = ord(u) - ord('A')
14+
v = ord(v) - ord('A')
15+
matrix[u][v] = 1 # True or False in case of edge
16+
matrix[v][u] = 1
17+
'''
18+
for i in range(e):
19+
u,v,w = map(str,input().split())
20+
u = ord(u) - ord('A')
21+
v = ord(v) - ord('A')
22+
w = int(w)
23+
matrix[u][v] = w # Weight / Cost in case of edge
24+
25+
26+
printmatrix(matrix)

bfs.py.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from collections import deque
2+
from collections import defaultdict
3+
4+
5+
'''
6+
V E
7+
FOR EVERY EDGE
8+
U V
9+
7 9
10+
A B
11+
A C
12+
A F
13+
C E
14+
C F
15+
C D
16+
D E
17+
D G
18+
G F
19+
'''
20+
# T.C = O(V+)
21+
# S.C = O(V)
22+
def bfs(graph,start,visited,path):
23+
queue = deque()
24+
path.append(start)
25+
queue.append(start)
26+
visited[start] = True
27+
while len(queue) != 0:
28+
tmpnode = queue.popleft()
29+
for neighbour in graph[tmpnode]:
30+
if visited[neighbour] == False:
31+
path.append(neighbour)
32+
queue.append(neighbour)
33+
visited[neighbour] = True
34+
return path
35+
36+
graph = defaultdict(list)
37+
v,e = map(int,input().split())
38+
for i in range(e):
39+
u,v = map(str,input().split())
40+
graph[u].append(v)
41+
graph[v].append(u)
42+
43+
start = 'A'
44+
path = []
45+
visited = defaultdict(bool)
46+
traversedpath = bfs(graph,start,visited,path)
47+
print(traversedpath)

dfs.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from collections import defaultdict
2+
3+
'''
4+
V E
5+
FOR EVERY EDGE
6+
U V
7+
7 9
8+
A B
9+
A C
10+
A F
11+
C E
12+
C F
13+
C D
14+
D E
15+
D G
16+
G F
17+
18+
'''
19+
# T.C = O(V+E)
20+
# S.C = O(V)
21+
def dfs(graph,start,visited,path):
22+
path.append(start)
23+
visited[start] = True
24+
for neighbour in graph[start]: # O(V+E)
25+
if visited[neighbour] == False: # O(1)
26+
dfs(graph,neighbour,visited,path)
27+
return path
28+
29+
30+
31+
v,e = map(int,input().split())
32+
graph = defaultdict(list)
33+
for i in range(e):
34+
u,v = map(str,input().split())
35+
graph[u].append(v)
36+
graph[v].append(u)
37+
38+
path = []
39+
start = 'A'
40+
visited = defaultdict(bool)
41+
traversedpath = dfs(graph,start,visited,path)
42+
print(traversedpath)

primalitytest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def approach2(n):
2323
return True
2424
if n%2 == 0 or n%3 == 0:# O(1)
2525
return False
26-
for i in range(5,int(sqrt(n))+1): # [1,root n]
26+
for i in range(5,int(sqrt(n))+1,6): # [1,root n] prime numbers are in the form of 6k-1 or 6k+1
2727
if n%i == 0 or n%(i+2) == 0:
2828
return False
2929
return True

0 commit comments

Comments
 (0)