-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData_Structure_Modified_Graph.py
More file actions
54 lines (41 loc) · 1.45 KB
/
Data_Structure_Modified_Graph.py
File metadata and controls
54 lines (41 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import pprint
class Vertex:
def __init__(self, name):
self.name = name
self.out_edges = [] # List to store outgoing edges (directed connections)
def add_edge(self, vertex):
self.out_edges.append(vertex)
class Graph:
def __init__(self):
self.graph = {} # Dictionary to store vertices and their outgoing edges
def add_vertex(self, vertex):
self.graph[vertex.name] = vertex.out_edges
def print_graph(self):
pprint.pprint(self.graph)
# Create vertices
V1 = Vertex("1")
V2 = Vertex("2")
V3 = Vertex("3")
V4 = Vertex("4")
# Get user input for directed connections
while True:
from_vertex = input("Enter the starting vertex (or 'done' to finish): ")
if from_vertex.lower() == "done":
break
to_vertex = input("Enter the ending vertex for this connection: ")
# Error handling for invalid vertex names
if from_vertex not in [V1.name, V2.name, V3.name, V4.name] or to_vertex not in [V1.name, V2.name, V3.name, V4.name]:
print("Invalid vertex name. Please enter a valid vertex name from 1, 2, 3, or 4.")
continue
# Add directed connection based on user input
V1.add_edge(eval(from_vertex)) # Assuming user enters vertex names (e.g., V1.add_edge(V2))
# Create graph object
g1 = Graph()
# Add vertices to the graph
g1.add_vertex(V1)
g1.add_vertex(V2)
g1.add_vertex(V3)
g1.add_vertex(V4)
# Print the directed graph
print("\nDirected Graph:")
g1.print_graph()