forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
977 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,32 @@ | ||
class Graph: | ||
class GRAPH: | ||
"""docstring for GRAPH""" | ||
def __init__(self, nodes): | ||
self.nodes=nodes | ||
self.graph=[[0]*nodes for i in range (nodes)] | ||
self.visited=[0]*nodes | ||
|
||
def __init__(self, vertex): | ||
self.vertex = vertex | ||
self.graph = [[0] * vertex for i in range(vertex) ] | ||
self.visited = [False] * vertex | ||
|
||
def add_edge(self, u, v): | ||
self.graph[u - 1][v - 1] = 1 | ||
self.graph[v - 1][u - 1] = 1 | ||
def show(self): | ||
print self.graph | ||
|
||
for i in self.graph: | ||
for j in i: | ||
print(j, end=' ') | ||
print(' ') | ||
def add_edge(self, i, j): | ||
self.graph[i][j]=1 | ||
self.graph[j][i]=1 | ||
|
||
|
||
def dfs(self, u): | ||
self.visited[u - 1] = True | ||
print('%d visited' % u) | ||
for i in range(1, self.vertex + 1): | ||
if self.graph[u - 1][i - 1] == 1 and self.visited[i - 1] == False: | ||
def dfs(self,s): | ||
self.visited[s]=1 | ||
print(s) | ||
for i in range(0,self.nodes): | ||
if self.visited[i]==0 and self.graph[s][i]==1: | ||
self.dfs(i) | ||
|
||
|
||
|
||
g = Graph(5) | ||
g.add_edge(1,4) | ||
g.add_edge(4,2) | ||
g.add_edge(4,5) | ||
g.add_edge(2,5) | ||
g.add_edge(5,3) | ||
g.dfs(1) | ||
n=int(input("Enter the number of Nodes : ")) | ||
g=GRAPH(n) | ||
e=int(input("Enter the no of edges : ")) | ||
print("Enter the edges (u v)") | ||
for i in range(0,e): | ||
u,v=map(int, raw_input().split()) | ||
g.add_edge(u,v) | ||
s=int(input("Enter the source node :")) | ||
g.dfs(s) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
''' | ||
A AVL tree | ||
''' | ||
|
||
|
||
class Node: | ||
|
||
def __init__(self, label): | ||
self.label = label | ||
self.left = None | ||
self.rigt = None | ||
self.parent = None | ||
self.height = 0 | ||
|
||
def getLabel(self): | ||
return self.label | ||
|
||
def setLabel(self, label): | ||
self.label = label | ||
|
||
def getLeft(self): | ||
return self.left | ||
|
||
def setLeft(self, left): | ||
self.left = left | ||
|
||
def getRight(self): | ||
return self.rigt | ||
|
||
def setRight(self, right): | ||
self.rigt = right | ||
|
||
def getParent(self): | ||
return self.parent | ||
|
||
def setParent(self, parent): | ||
self.parent = parent | ||
|
||
def setHeight(self, height): | ||
self.height = height | ||
|
||
def getHeight(self, height): | ||
return self.height | ||
|
||
|
||
class AVL: | ||
|
||
def __init__(self): | ||
self.root = None | ||
self.size = 0 | ||
|
||
def insert(self, value): | ||
node = Node(value) | ||
if self.root is None: | ||
self.root = node | ||
self.size = 1 | ||
else: | ||
# Same as Binary Tree | ||
dad_node = None | ||
curr_node = self.root | ||
|
||
while True: | ||
if curr_node is not None: | ||
|
||
dad_node = curr_node | ||
|
||
if node.getLabel() < curr_node.getLabel(): | ||
curr_node = curr_node.getLeft() | ||
else: | ||
curr_node = curr_node.getRight() | ||
else: | ||
if node.getLabel() < dad_node.getLabel(): | ||
dad_node.setLeft(node) | ||
dad_node.setHeight(dad_node.getHeight() + 1) | ||
|
||
if (dad_node.getRight().getHeight() - | ||
dad_node.getLeft.getHeight() > 1): | ||
self.rebalance(dad_node) | ||
|
||
else: | ||
dad_node.setRight(node) | ||
dad_node.setHeight(dad_node.getHeight() + 1) | ||
|
||
if (dad_node.getRight().getHeight() - | ||
dad_node.getLeft.getHeight() > 1): | ||
self.rebalance(dad_node) | ||
break | ||
|
||
def rebalance(self, node): | ||
if (node.getRight().getHeight() - | ||
node.getLeft.getHeight() > 1): | ||
if (node.getRight().getHeight() > | ||
node.getLeft.getHeight()): | ||
pass | ||
else: | ||
pass | ||
pass | ||
elif (node.getRight().getHeight() - | ||
node.getLeft.getHeight() > 2): | ||
if (node.getRight().getHeight() > | ||
node.getLeft.getHeight()): | ||
pass | ||
else: | ||
pass | ||
pass | ||
pass | ||
|
||
def rotate_left(self, node): | ||
# TODO: is this pythonic enought? | ||
aux = node.getLabel() | ||
node = aux.getRight() | ||
node.setHeight(node.getHeight() - 1) | ||
node.setLeft(Node(aux)) | ||
node.getLeft().setHeight(node.getHeight() + 1) | ||
node.getRight().setHeight(node.getRight().getHeight() - 1) | ||
|
||
def rotate_right(self, node): | ||
aux = node.getLabel() | ||
node = aux.getLeft() | ||
node.setHeight(node.getHeight() - 1) | ||
node.setRight(Node(aux)) | ||
node.getLeft().setHeight(node.getHeight() + 1) | ||
node.getLeft().setHeight(node.getLeft().getHeight() - 1) | ||
|
||
def double_rotate_left(self, node): | ||
self.rotate_right(node.getRight().getRight()) | ||
self.rotate_left(node) | ||
|
||
def double_rotate_right(self, node): | ||
self.rotate_left(node.getLeft().getLeft()) | ||
self.rotate_right(node) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Author: OMKAR PATHAK | ||
|
||
# We can use Python's dictionary for constructing the graph | ||
|
||
class AdjacencyList(object): | ||
def __init__(self): | ||
self.List = {} | ||
|
||
def addEdge(self, fromVertex, toVertex): | ||
# check if vertex is already present | ||
if fromVertex in self.List.keys(): | ||
self.List[fromVertex].append(toVertex) | ||
else: | ||
self.List[fromVertex] = [toVertex] | ||
|
||
def printList(self): | ||
for i in self.List: | ||
print(i,'->',' -> '.join([str(j) for j in self.List[i]])) | ||
|
||
if __name__ == '__main__': | ||
al = AdjacencyList() | ||
al.addEdge(0, 1) | ||
al.addEdge(0, 4) | ||
al.addEdge(4, 1) | ||
al.addEdge(4, 3) | ||
al.addEdge(1, 0) | ||
al.addEdge(1, 4) | ||
al.addEdge(1, 3) | ||
al.addEdge(1, 2) | ||
al.addEdge(2, 3) | ||
al.addEdge(3, 4) | ||
|
||
al.printList() | ||
|
||
# OUTPUT: | ||
# 0 -> 1 -> 4 | ||
# 1 -> 0 -> 4 -> 3 -> 2 | ||
# 2 -> 3 | ||
# 3 -> 4 | ||
# 4 -> 1 -> 3 |
Oops, something went wrong.