-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLCS_Rhash.py
More file actions
97 lines (74 loc) · 1.99 KB
/
LCS_Rhash.py
File metadata and controls
97 lines (74 loc) · 1.99 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'''
This script will create a simple custom hash map and use
the number of LCS recursions from LCSRec for each value
as a key.
The hash map is then graphed to show how many collisions
occured in the hash map.
'''
# picked 5000 as the limit
import time
import plotly.graph_objects as go
class RecsAndLen:
recCalls = 0
lcsNum = 0
class MyHashTable:
chains = []
def __init__(self):
for i in range(10000): # originally 1000000
newChain = []
self.chains.append(newChain)
def addThis(self, ral, actNum):
if ral.recCalls > 5000:
ral.recCalls %= 5000
self.chains[ral.recCalls].append(actNum)
def LCSRec(str1, str2, place1, place2):
lcsInt = 0
recInt = 0
ral = RecsAndLen()
if place1 > len(str1) - 1:
ral.recCalls = 0
ral.lcsNum = 0
return ral
if place2 > len(str2) - 1:
ral.recCalls = 0
ral.lcsNum = 0
return ral
if str1[place1] == str2[place2]:
nextCall = LCSRec(str1, str2, place1 + 1, place2 + 1)
lcsInt = 1 + nextCall.lcsNum
recInt = 1 + nextCall.recCalls
else:
check1 = LCSRec(str1, str2, place1 + 1, place2)
check2 = LCSRec(str1, str2, place1, place2 + 1)
lcsInt = max(check1.lcsNum, check2.lcsNum)
recInt = 1 + check1.recCalls + check2.recCalls
ral.recCalls = recInt;
ral.lcsNum = lcsInt;
return ral
return lcsInt;
# make list
fullList = []
s = 100000
for i in range(9000): # orignally 900000
fullList.append(s)
s += 1
# string for comparison
strc = "0123456789"
# make hash table
mht = MyHashTable()
# get hashes and add to hash table
for i in fullList:
actNum = i
ral = LCSRec(strc, str(i), 0, 0)
mht.addThis(ral, actNum)
# print graph
yset = []
xset = []
counter = 0;
for i in mht.chains:
if len(i) > 0:
yset.append(len(i))
xset.append(counter)
counter += 1
fig = go.Figure(data=go.Bar(x=xset, y=yset))
fig.show()