20BCS7358 CC Exp.5
20BCS7358 CC Exp.5
20BCS7358 CC Exp.5
Consider an undirected graph where each edge weighs 6 units. Each of the nodes is labeled
consecutively from 1 to n.You will be given a number of queries. For each query, you will be given a
list of edges describing an undirected graph. After you create a representation of the graph, you must
determine and report the shortest distance to each of the other nodes from a given starting position using
the breadth-first search algorithm (BFS). Return an array of distances from the start node in node
number order. If a node is unreachable, return -1 for that node.
You are given a tree (a simple connected graph with no cycles).Find the maximum number of edges you
can remove from the tree to get a forest such that each connected component of the forest contains an
even number of nodes.As an example, the following tree with 4 nodes can be cut at most1 time to create
an even forest.
3. Requirement Analysis :-
BFS
import queue
t = int(input())
for i in range(t):
v, e = map(int, input().split())
l = [[0 for x in range(v)] for y in range(v)]
for j in range(e):
a, b = map(int, input().split())
l[a - 1][b - 1] = l[b - 1][a - 1] = 1
s = int(input())
q.put(s - 1)
visited[s - 1] = 1
distance[s - 1] = 0
for j in range(v):
if j != s - 1:
print(distance[j], end = ' ')
print()
Even Tree:
for i in range(te):
tf, tt = map(int, input().rstrip().split())
if tt not in tr.keys(): tr[tt] = [tf]
else: tr[tt].append(tf)
ch[i] = 1
print(c)
5. Output:
BFS
Even Tree
Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):