Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8490a3a

Browse files
Directed Graph using Adjacency Matrix
1 parent 8110022 commit 8490a3a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Simple Graph Implementation
2+
3+
class Graph:
4+
def __init__(self, number_of_nodes): # Initialise Graph Data Structure
5+
self.number_of_nodes = number_of_nodes + 1 # Number of nodes will have to be increased by one
6+
self.graph = [[0 for x in range(number_of_nodes + 1)] for y in range(number_of_nodes + 1)]
7+
8+
def withInBounds(self, v1, v2): # Function to check if the value is within matrix bounds
9+
return v1 >= 0 and v1 <= self.number_of_nodes and v2 >= 0 and v2 <= self.number_of_nodes
10+
11+
def insertEdge(self, v1, v2): # Function to inser edge in the Graph
12+
if self.withInBounds(v1, v2): # Check if values are within bounds
13+
self.graph[v1][v2] = 1 # Change the value of the node from 0 to 1
14+
15+
def printGraph(self): # Functipon to Print Graph
16+
for i in range(self.number_of_nodes):
17+
for j in range(len(self.graph[i])):
18+
if self.graph[i][j]:
19+
print(i, '=>', j)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /