-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursiveCycleCounter.py
67 lines (60 loc) · 1.94 KB
/
recursiveCycleCounter.py
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
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 7 12:17:45 2019
@author: Cullen
"""
from copy import deepcopy
import time
def pList(l):
for i in l:
print("{}:".format(i), l[i])
print()
def recCount(neighborList, currVert = None):
count = 0
if currVert == None:
for vertex in neighborList:
count += recCount(deepcopy(neighborList), vertex)
return count
else:
oList = deepcopy(neighborList)
if len(neighborList[currVert]) == 0:
for i in neighborList:
if len(neighborList[i]) != 0:
return 0
return 1
for path in oList[currVert]:
neighborList[currVert].remove(path)
neighborList[path].remove(currVert)
count += recCount(deepcopy(neighborList), path)
neighborList = deepcopy(oList)
return count
def genNeighborList(cycle):
neighborList = {}
for i in range(len(cycle)-1):
if i == 0:
neighborList[cycle[0]] = [cycle[1]]
neighborList[cycle[1]] = [cycle[0]]
continue
try:
neighborList[cycle[i]].append(cycle[i+1])
except KeyError:
neighborList[cycle[i]] = [cycle[i+1]]
try:
neighborList[cycle[i+1]].append(cycle[i])
except KeyError:
neighborList[cycle[i+1]] = [cycle[i]]
return neighborList
def output(cycle):
neighborList = genNeighborList(cycle[1:])
sTime = time.time()
count = recCount(deepcopy(neighborList), cycle[1]) * 2 * (len(cycle) - 1)
fTime = time.time()
return count, (fTime-sTime)
if __name__ == "__main__":
cycle = input("Cycle: ")
neighborList = genNeighborList(cycle[1:])
sTime = time.time()
count = recCount(deepcopy(neighborList), cycle[1]) * 2 * (len(cycle) - 1)
fTime = time.time()
print(count)
print(fTime - sTime)