Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
frmatias authored Aug 28, 2017
2 parents 1bc3a17 + 75ccf5b commit 6ee6f12
Show file tree
Hide file tree
Showing 19 changed files with 977 additions and 43 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ python:
- "3.5"
- "3.6"
- "3.6-dev"
- "3.7-dev"
- "nightly"

install:
- if [ "$TRAVIS_PYTHON_VERSION" == "3.2" ]; then travis_retry pip install coverage==3.7.1; fi
- if [ "$TRAVIS_PYTHON_VERSION" != "3.2" ]; then travis_retry pip install coverage; fi
Expand Down
41 changes: 33 additions & 8 deletions Graphs/Breadth_First_Search.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
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) ]

def add_edge(self, u, v):
self.graph[u - 1][v - 1] = 1
self.graph[v - 1][u - 1] = 1

def show(self):

Expand Down Expand Up @@ -43,3 +41,30 @@ def bfs(self,v):
g.add_edge(5,9)
g.add_edge(6,10)
g.bfs(4)
=======
print self.graph

def add_edge(self, i, j):
self.graph[i][j]=1
self.graph[j][i]=1

def bfs(self,s):
queue=[s]
self.visited[s]=1
while len(queue)!=0:
x=queue.pop(0)
print(x)
for i in range(0,self.nodes):
if self.graph[x][i]==1 and self.visited[i]==0:
queue.append(i)
self.visited[i]=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.bfs(s)
51 changes: 25 additions & 26 deletions Graphs/Deep_First_Search.py
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)
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ __Properties__
### Shell
![alt text][shell-image]

From [Wikipedia][shell-wiki]: Shellsort is a generalization of insertion sort that allows the exchange of items that are far apart. The idea is to arrange the list of elements so that, starting anywherem considereing every nth element gives a sorted list. Such a list is said to be h-sorted. Equivanelty, it can be thought of as h intterleaved lists, each individually sorted.
From [Wikipedia][shell-wiki]: Shellsort is a generalization of insertion sort that allows the exchange of items that are far apart. The idea is to arrange the list of elements so that, starting anywhere, considering every nth element gives a sorted list. Such a list is said to be h-sorted. Equivalently, it can be thought of as h interleaved lists, each individually sorted.

__Properties__
* Worst case performance O(nlog2 2n)
Expand All @@ -83,7 +83,7 @@ __Properties__

###### View the algorithm in [action][shell-toptal]

###Time-Compexity Graphs
### Time-Compexity Graphs

Comparing the complexity of sorting algorithms (Bubble Sort, Insertion Sort, Selection Sort)

Expand Down
131 changes: 131 additions & 0 deletions data_structures/AVL/AVL.py
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)
12 changes: 7 additions & 5 deletions data_structures/Binary Tree/binary_seach_tree.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'''
A binary search Tree
'''


class Node:

def __init__(self, label):
Expand All @@ -12,7 +14,7 @@ def getLabel(self):
return self.label

def setLabel(self, label):
self.label = label
self.label = label

def getLeft(self):
return self.left
Expand All @@ -34,7 +36,7 @@ def __init__(self):

def insert(self, label):

#Create a new Node
# Create a new Node

node = Node(label)

Expand All @@ -45,7 +47,7 @@ def insert(self, label):
curr_node = self.root

while True:
if curr_node != None:
if curr_node is not None:

dad_node = curr_node

Expand All @@ -61,12 +63,12 @@ def insert(self, label):
break

def empty(self):
if self.root == None:
if self.root is None:
return True
return False

def preShow(self, curr_node):
if curr_node != None:
if curr_node is None:
print(curr_node.getLabel(), end=" ")

self.preShow(curr_node.getLeft())
Expand Down
40 changes: 40 additions & 0 deletions data_structures/Graph/Graph.py
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
Loading

0 comments on commit 6ee6f12

Please sign in to comment.