Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
TME_AAGB_2019
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Laurent David
TME_AAGB_2019
Commits
25818f59
Commit
25818f59
authored
Dec 13, 2019
by
Laurent David
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
8360289b
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
88 additions
and
0 deletions
+88
-0
BreakPointReversalCode
TME4/BreakPointReversalCode
+88
-0
No files found.
TME4/BreakPointReversalCode
0 → 100644
View file @
25818f59
import random
def makePermutation(n):
""" generates a random permuation of the numbers 1..n-1 sandwiched between 0 and n """
seq = range(1,n)
random.shuffle(seq)
return [0] + seq + [n]
def hasBreakpoints(seq):
""" returns True if sequnces in not strictly increasing by 1 """
for i in xrange(1, len(seq)):
if (seq[i] != seq[i-1] + 1):
return True
return False
def getStrips(seq):
""" find contained intervals where sequence is ordered, and return intervals
in as lists, increasing and decreasing. Single elements are considered
decreasing. "Contained" excludes the first and last interval. """
deltas = [seq[i+1] - seq[i] for i in xrange(len(seq)-1)]
increasing = list()
decreasing = list()
start = 0
for i, diff in enumerate(deltas):
if (abs(diff) == 1) and (diff == deltas[start]):
continue
if (start > 0):
if deltas[start] == 1:
increasing.append((start, i+1))
else:
decreasing.append((start, i+1))
start = i+1
return increasing, decreasing
def pickReversal(seq, strips):
""" test each decreasing interval to see if it leads to a reversal that
removes two breakpoints, otherwise, return a reversal that removes only one """
reversal = (-1, None)
left = [i for i, j in strips]
right = [j for i, j in strips]
for i in left:
for j in right:
if (i >= j-1):
# skip invalid intervals and
# those with only one element
continue
breakpointsRemoved = 0
if (abs(seq[j-1] - seq[i-1]) == 1):
# reversal will remove left breakpoint
breakpointsRemoved += 1
if (abs(seq[j] - seq[i]) == 1):
# reversal will remove right breakpoint
breakpointsRemoved += 1
if (breakpointsRemoved > reversal[0]):
reversal = (breakpointsRemoved, (i,j))
print "%d:" % reversal[0],
return reversal[1]
def doReversal(seq,(i,j)):
return seq[:i] + [element for element in reversed(seq[i:j])] + seq[j:]
def improvedBreakpointReversalSort(seq):
while hasBreakpoints(seq):
increasing, decreasing = getStrips(seq)
if len(decreasing) > 0:
reversal = pickReversal(seq, increasing+decreasing)
else:
print "0:",
reversal = increasing[0]
print seq, "reversal", reversal
seq = doReversal(seq,reversal)
print seq, "Sorted"
return
if __name__ == "__main__":
print "Python implementation of breakpoint reversal sort on page 135"
while True:
input = raw_input('Enter a list, the size of list, or 0 to exit:')
if (input.find(',') > 0):
L = [int(v) for v in input.split(',')]
else:
n = int(input)
if (n == 0):
break
L = makePermutation(n)
improvedBreakpointReversalSort(L)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment