Skip to content

Commit d668c17

Browse files
authored
Refactor graph_initialization at basic_graph.py (#4601)
1 parent 63ac09e commit d668c17

File tree

1 file changed

+62
-35
lines changed

1 file changed

+62
-35
lines changed

graphs/basic_graphs.py

+62-35
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,69 @@
11
from collections import deque
22

3+
4+
def _input(message):
5+
return input(message).strip().split(" ")
6+
7+
8+
def initialize_unweighted_directed_graph(
9+
node_count: int, edge_count: int
10+
) -> dict[int, list[int]]:
11+
graph: dict[int, list[int]] = {}
12+
for i in range(node_count):
13+
graph[i + 1] = []
14+
15+
for e in range(edge_count):
16+
x, y = [int(i) for i in _input(f"Edge {e + 1}: <node1> <node2> ")]
17+
graph[x].append(y)
18+
return graph
19+
20+
21+
def initialize_unweighted_undirected_graph(
22+
node_count: int, edge_count: int
23+
) -> dict[int, list[int]]:
24+
graph: dict[int, list[int]] = {}
25+
for i in range(node_count):
26+
graph[i + 1] = []
27+
28+
for e in range(edge_count):
29+
x, y = [int(i) for i in _input(f"Edge {e + 1}: <node1> <node2> ")]
30+
graph[x].append(y)
31+
graph[y].append(x)
32+
return graph
33+
34+
35+
def initialize_weighted_undirected_graph(
36+
node_count: int, edge_count: int
37+
) -> dict[int, list[tuple[int, int]]]:
38+
graph: dict[int, list[tuple[int, int]]] = {}
39+
for i in range(node_count):
40+
graph[i + 1] = []
41+
42+
for e in range(edge_count):
43+
x, y, w = [int(i) for i in _input(f"Edge {e + 1}: <node1> <node2> <weight> ")]
44+
graph[x].append((y, w))
45+
graph[y].append((x, w))
46+
return graph
47+
48+
349
if __name__ == "__main__":
4-
# Accept No. of Nodes and edges
5-
n, m = map(int, input().split(" "))
50+
n, m = [int(i) for i in _input("Number of nodes and edges: ")]
51+
52+
graph_choice = int(
53+
_input(
54+
"Press 1 or 2 or 3 \n"
55+
"1. Unweighted directed \n"
56+
"2. Unweighted undirected \n"
57+
"3. Weighted undirected \n"
58+
)[0]
59+
)
60+
61+
g = {
62+
1: initialize_unweighted_directed_graph,
63+
2: initialize_unweighted_undirected_graph,
64+
3: initialize_weighted_undirected_graph,
65+
}[graph_choice](n, m)
666

7-
# Initialising Dictionary of edges
8-
g = {}
9-
for i in range(n):
10-
g[i + 1] = []
11-
12-
"""
13-
----------------------------------------------------------------------------
14-
Accepting edges of Unweighted Directed Graphs
15-
----------------------------------------------------------------------------
16-
"""
17-
for _ in range(m):
18-
x, y = map(int, input().strip().split(" "))
19-
g[x].append(y)
20-
21-
"""
22-
----------------------------------------------------------------------------
23-
Accepting edges of Unweighted Undirected Graphs
24-
----------------------------------------------------------------------------
25-
"""
26-
for _ in range(m):
27-
x, y = map(int, input().strip().split(" "))
28-
g[x].append(y)
29-
g[y].append(x)
30-
31-
"""
32-
----------------------------------------------------------------------------
33-
Accepting edges of Weighted Undirected Graphs
34-
----------------------------------------------------------------------------
35-
"""
36-
for _ in range(m):
37-
x, y, r = map(int, input().strip().split(" "))
38-
g[x].append([y, r])
39-
g[y].append([x, r])
4067

4168
"""
4269
--------------------------------------------------------------------------------

0 commit comments

Comments
 (0)