-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgraph.py
More file actions
67 lines (49 loc) · 1.51 KB
/
graph.py
File metadata and controls
67 lines (49 loc) · 1.51 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
55
56
57
58
59
60
61
62
63
64
65
66
### Graph Theory Datastructure
# Helper: https://www.python-course.eu/graphs_python.php
# * Directed graph -> no reversible flow, the flow or edge remains in single direction
# * UnDirected graph -> reversible flow, the flow or edge in on both the direction
## Represent a Graph with list and dictionary
# Undirected
graph1 = {
"node1": ["node2", "node4"],
"node2": ["node1", "node3"],
"node3": ["node2"],
"node4": ["node1"]
}
# If nodes are index of a list then we can use list representation
graph2 = [[2, 4], [1, 3], [2], [1]]
# Directed
graph3 = {
"node1": ["node2", "node4"],
"node2": ["node3"],
"node3": ["node4"],
"node4": []
}
graph4 = [[2, 4], [3], [4], []]
## Create a Graph - when given a list of relationships
example_relations = [("c1","c2"), ("c2","c3"), ("c1","c4"), ("c2","c1"), ("c3","c2"), ("c4","c1")]
graph5 = {}
for relation in example_relations:
node1 = relation[0]
node2 = relation[1]
if node1 not in graph5:
graph5[node1] = []
if node2 not in graph5:
graph5[node2] = []
if node2 not in graph5[node1]:
graph5[node1].append(node2)
print(graph5)
## Get Edges and Vertices
nodes = graph3.keys()
edges = []
for node in graph3.keys():
for node2 in graph3[node]:
edges.append((node, node2))
print(nodes, edges)
## Breath First Traverse or Topological Sort
# Iterative
# Recursive
## Depth First Traverse Graph
# Iterative
# Recursive
## Traverse through all the nodes of the Graph - Give relative ordering of all the nodes