-
Notifications
You must be signed in to change notification settings - Fork 302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SkipList.py: Added SkipList Data Structure Implementation #236
Changes from 1 commit
80acf5f
ec315c7
d08032f
87ff559
c513b30
8672c3b
2f1a008
7c4d0f0
fb37b4d
8eda6e0
0f9c89a
1e8490f
992047c
cce5f66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
''' | ||
Skip list is a probabilistic data structure that allows efficient search, insertion and removal operations. | ||
It allows fast search within an ordered sequence of elements, O(log(n)) complexity. | ||
|
||
Wikipedia Page: https://en.wikipedia.org/wiki/Skip_list | ||
''' | ||
|
||
from random import randint, seed | ||
import time | ||
|
||
|
||
class SkipNode: | ||
''' | ||
A single node in a SkipList data structure, which is similar to a normal linked list node. | ||
''' | ||
def __init__(self, height = 0, elem = None): | ||
self.elem = elem | ||
self.next = [None]*height | ||
|
||
class SkipList: | ||
|
||
def __init__(self): | ||
|
||
# This will set the head node of the list. | ||
self.head = SkipNode() | ||
self.len = 0 | ||
self.maxHeight = 0 | ||
|
||
def __len__(self): | ||
return self.len | ||
|
||
''' | ||
The Search operation takes O(log(n)) time complexity as compared to O(n) in classical linked list. | ||
''' | ||
def find(self, elem, update = None): | ||
if update == None: | ||
update = self.updateList(elem) | ||
if len(update) > 0: | ||
candidate = update[0].next[0] | ||
if candidate != None and candidate.elem == elem: | ||
return candidate | ||
return None | ||
|
||
''' | ||
This function returns a randomized hieght for insertion algorithm. | ||
''' | ||
def randomHeight(self): | ||
height = 1 | ||
while randint(1, 2) != 1: | ||
height += 1 | ||
return height | ||
|
||
''' | ||
This function returns the skip list that is below the element | ||
which is closest and smaller than the element that is required to find. | ||
''' | ||
def updateList(self, elem): | ||
update = [None]*self.maxHeight | ||
x = self.head | ||
for i in reversed(range(self.maxHeight)): | ||
while x.next[i] != None and x.next[i].elem < elem: | ||
x = x.next[i] | ||
update[i] = x | ||
return update | ||
|
||
''' | ||
This function inserts an element in the correct possition. | ||
The insertion operation takes O(n) time complexity | ||
''' | ||
|
||
def insert(self, elem): | ||
node = SkipNode(self.randomHeight(), elem) | ||
self.maxHeight = max(self.maxHeight, len(node.next)) | ||
while len(self.head.next) < len(node.next): | ||
self.head.next.append(None) | ||
update = self.updateList(elem) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line contains following spacing inconsistencies:
Origin: SpaceConsistencyBear, Section: The issue can be fixed by applying the following patch: --- a/tmp/tmpphsfotv0/skip_list/Python/SkipList.py
+++ b/tmp/tmpphsfotv0/skip_list/Python/SkipList.py
@@ -73,7 +73,7 @@
self.maxHeight = max(self.maxHeight, len(node.next))
while len(self.head.next) < len(node.next):
self.head.next.append(None)
- update = self.updateList(elem)
+ update = self.updateList(elem)
if self.find(elem, update) == None:
for i in range(len(node.next)):
node.next[i] = update[i].next[i] |
||
if self.find(elem, update) == None: | ||
for i in range(len(node.next)): | ||
node.next[i] = update[i].next[i] | ||
update[i].next[i] = node | ||
self.len += 1 | ||
|
||
''' | ||
This function will remove the specified element from the list. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line contains following spacing inconsistencies:
Origin: SpaceConsistencyBear, Section: The issue can be fixed by applying the following patch: --- a/tmp/tmpphsfotv0/skip_list/Python/SkipList.py
+++ b/tmp/tmpphsfotv0/skip_list/Python/SkipList.py
@@ -81,7 +81,7 @@
self.len += 1
'''
- This function will remove the specified element from the list.
+ This function will remove the specified element from the list.
The deletion operation in Skip List takes O(log(n)) time.
'''
def remove(self, elem): |
||
The deletion operation in Skip List takes O(log(n)) time. | ||
''' | ||
def remove(self, elem): | ||
update = self.updateList(elem) | ||
x = self.find(elem, update) | ||
if x != None: | ||
for i in reversed(range(len(x.next))): | ||
update[i].next[i] = x.next[i] | ||
if self.head.next[i] == None: | ||
self.maxHeight -= 1 | ||
self.len -= 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line contains following spacing inconsistencies:
Origin: SpaceConsistencyBear, Section: The issue can be fixed by applying the following patch: --- a/tmp/tmpphsfotv0/skip_list/Python/SkipList.py
+++ b/tmp/tmpphsfotv0/skip_list/Python/SkipList.py
@@ -92,7 +92,7 @@
update[i].next[i] = x.next[i]
if self.head.next[i] == None:
self.maxHeight -= 1
- self.len -= 1
+ self.len -= 1
'''
This function will print the entier Data Structure, level by level (top to bottom) |
||
|
||
''' | ||
This function will print the entier Data Structure, level by level (top to bottom) | ||
''' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line contains following spacing inconsistencies:
Origin: SpaceConsistencyBear, Section: The issue can be fixed by applying the following patch: --- a/tmp/tmpphsfotv0/skip_list/Python/SkipList.py
+++ b/tmp/tmpphsfotv0/skip_list/Python/SkipList.py
@@ -96,7 +96,7 @@
'''
This function will print the entier Data Structure, level by level (top to bottom)
- '''
+ '''
def printList(self):
for i in range(len(self.head.next)-1, -1, -1):
x = self.head |
||
def printList(self): | ||
for i in range(len(self.head.next)-1, -1, -1): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line contains following spacing inconsistencies:
Origin: SpaceConsistencyBear, Section: The issue can be fixed by applying the following patch: --- a/tmp/tmphyrtd5og/skip_list/Python/SkipList.py
+++ b/tmp/tmphyrtd5og/skip_list/Python/SkipList.py
@@ -56,7 +56,7 @@
while x.next[i]!=None and x.next[i].elem<elem:
x=x.next[i]
update[i]=x
- return update
+ return update
'''
This function inserts an element in the correct possition.
The insertion operation takes O(n) time complexity |
||
x = self.head | ||
while x.next[i] != None: | ||
print x.next[i].elem, | ||
x = x.next[i] | ||
print '' | ||
|
||
if __name__ == "__main__": | ||
skipList = SkipList() | ||
|
||
print("Insertion Started") | ||
t0 = time.time() | ||
for i in range(1000000): | ||
skipList.insert(i) | ||
print("Time taken to insert 1 Million elements to the skip list: "+str(time.time() - t0)+" s.") | ||
print("Total Size = "+str(len(skipList))) | ||
# skipList.printList() | ||
|
||
t0 = time.time() | ||
# Skip List takes the maximum time to search the elements in the middle. | ||
print(skipList.find(596536) != None) | ||
print("Time taken to search in a list of size 1 Million: "+str(time.time() - t0)+" s.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line contains following spacing inconsistencies:
Origin: SpaceConsistencyBear, Section: The issue can be fixed by applying the following patch: --- a/tmp/tmpphsfotv0/skip_list/Python/SkipList.py
+++ b/tmp/tmpphsfotv0/skip_list/Python/SkipList.py
@@ -119,4 +119,4 @@
t0 = time.time()
# Skip List takes the maximum time to search the elements in the middle.
print(skipList.find(596536) != None)
- print("Time taken to search in a list of size 1 Million: "+str(time.time() - t0)+" s.")+ print("Time taken to search in a list of size 1 Million: "+str(time.time() - t0)+" s.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line contains following spacing inconsistencies:
Origin: SpaceConsistencyBear, Section:
all.pyjava
.The issue can be fixed by applying the following patch: