Skip to content

Commit cade535

Browse files
changed small part of code and updated readme
1 parent c088e1d commit cade535

File tree

3 files changed

+56
-51
lines changed

3 files changed

+56
-51
lines changed

Algorithms/dynamic_programming/sequence_alignment.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
# Algorithm for solving sequence alignment
2-
# Input strings x,y of len(x) = m, len(y) = n and find minimum number of
3-
# edit steps and the specific steps to transform x into y.
1+
'''
2+
Algorithm for solving sequence alignment
3+
Input strings x,y of len(x) = m, len(y) = n and find minimum number of
4+
edit steps and the specific steps to transform x into y.
45
5-
# Video of algorithm explanation: https://youtu.be/bQ7kRW6zo9Y
6-
# Video of code explanation: https://youtu.be/XmyxiSc3LKg
6+
Time Complexity: O(nm)
77
8-
# Programmed by Aladdin Persson <aladdin dot persson at hotmail dot com>
9-
# 2020-02-15 Initial coding
10-
# 2020-02-16 Improved find_solution and made code cleaner
11-
# 2020-03-13 There was an error in the code in function find_solution,
12-
# I was working with list indexing as if it was a matrix.
13-
# Should be working now. Extensive testing would be good.
14-
# 2020-03-28 Cleaned up code by making SequenceAlignment into class
8+
Video of algorithm explanation: https://youtu.be/bQ7kRW6zo9Y
9+
Video of code explanation: https://youtu.be/XmyxiSc3LKg
10+
11+
Programmed by Aladdin Persson <aladdin dot persson at hotmail dot com>
12+
2020-02-15 Initial coding
13+
2020-02-16 Improved find_solution and made code cleaner
14+
2020-03-13 There was an error in the code in function find_solution,
15+
I was working with list indexing as if it was a matrix.
16+
Should be working now. Extensive testing would be good.
17+
18+
2020-03-28 Cleaned up code by making SequenceAlignment into class
19+
'''
1520

1621
class SequenceAlignment(object):
1722
def __init__(self, x, y):
@@ -63,11 +68,11 @@ def alignment(self):
6368

6469
return (OPT[m][n], self.solution[::-1])
6570

66-
if __name__ == '__main__':
67-
x = 'TGACGTGC'
68-
y = 'TCGACGTCA'
69-
print('We we want to transform: ' + x + ' to: ' + y)
70-
sqalign = SequenceAlignment(x, y)
71-
min_edit, steps = sqalign.alignment()
72-
print('Minimum amount of edit steps are: ' + str(min_edit))
73-
print('And the way to do it is: ' + str(steps))
71+
# if __name__ == '__main__':
72+
# x = 'TGACGTGC'
73+
# y = 'TCGACGTCA'
74+
# print('We we want to transform: ' + x + ' to: ' + y)
75+
# sqalign = SequenceAlignment(x, y)
76+
# min_edit, steps = sqalign.alignment()
77+
# print('Minimum amount of edit steps are: ' + str(min_edit))
78+
# print('And the way to do it is: ' + str(steps))

Algorithms/dynamic_programming/weighted_interval_scheduling.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
# Weighted Interval Scheduling
2-
# Explained YouTube video: https://www.youtube.com/watch?v=iIX1YvbLbvc
3-
# Implementation walkthrough video: https://www.youtube.com/watch?v=dU-coYsd7zw
1+
'''
2+
Weighted Interval Scheduling
3+
Explained YouTube video: https://www.youtube.com/watch?v=iIX1YvbLbvc
4+
Implementation walkthrough video: https://www.youtube.com/watch?v=dU-coYsd7zw
45
5-
# Programmed by Aladdin Persson <aladdin.persson at hotmail dot com>
6-
# 2020-02-13 Initial programming
7-
# 2020-03-28 Cleaned up code by making WeightedIntervalScheduling class
6+
Programmed by Aladdin Persson <aladdin.persson at hotmail dot com>
7+
2020-02-13 Initial programming
8+
2020-03-28 Cleaned up code by making WeightedIntervalScheduling class
89
9-
# Time complexity: O(nlogn)
10+
Time complexity: O(nlogn)
11+
'''
1012

1113
import bisect
1214

@@ -64,21 +66,19 @@ def weighted_interval(self):
6466

6567
return self.OPT[-1], self.solution[::-1]
6668

67-
if __name__ == '__main__':
68-
# They are labeled as: (start, end, weight)
69-
t1 = (0,3,3)
70-
t2 = (1,4,2)
71-
t3 = (0,5,4)
72-
t4 = (3,6,1)
73-
t5 = (4,7,2)
74-
t6 = (3,9,5)
75-
t7 = (5,10,2)
76-
t8 = (8,10,1)
77-
I = [t1,t2,t3,t4,t5,t6,t7,t8]
78-
I = [(0, 50, 1), (0, 49, 1), (0, 48, 1), (15, 20, 10)] # --> [(15,20,10), (0,48,1), (0,49, 1), (0,50, 1)]
79-
weightedinterval = WeightedIntervalScheduling(I)
80-
max_weight, best_intervals = weightedinterval.weighted_interval()
81-
82-
#max_weight = weighted_interval(I)
83-
print('Maximum weight: ' + str(max_weight))
84-
print('The best items to take are: ' + str(best_intervals[::-1]))
69+
# Small Example
70+
# if __name__ == '__main__':
71+
# # They are labeled as: (start, end, weight)
72+
# t1 = (0,3,3)
73+
# t2 = (1,4,2)
74+
# t3 = (0,5,4)
75+
# t4 = (3,6,1)
76+
# t5 = (4,7,2)
77+
# t6 = (3,9,5)
78+
# t7 = (5,10,2)
79+
# t8 = (8,10,1)
80+
# I = [t1,t2,t3,t4,t5,t6,t7,t8]
81+
# weightedinterval = WeightedIntervalScheduling(I)
82+
# max_weight, best_intervals = weightedinterval.weighted_interval()
83+
# print('Maximum weight: ' + str(max_weight))
84+
# print('The best items to take are: ' + str(best_intervals))

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ Whenever I face an interesting problem I document the algorithm that I learned t
1212
* :white_check_mark: [:movie_camera:](https://youtu.be/dU-coYsd7zw)[Weighted Interval Scheduling](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/dynamic_programming/weighted_interval_scheduling.py) **- O(nlog(n))**
1313

1414
# Graph theory
15-
* :white_check_mark: [Kahns Topological Sort](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/graphtheory/kahns-toposort/kahns.py) **- O(n + m)**
16-
* :white_check_mark: [Bellman-Ford Shortest Path](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/graphtheory/bellman-ford/bellman_ford.py) **-O(mn)**
15+
* :white_check_mark: [Kahns Topological Sort](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/graphtheory/kahns-toposort/kahn_topological_ordering.py) **- O(n + m)**
16+
* :white_check_mark: [Bellman-Ford Shortest Path](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/graphtheory/bellman-ford/bellman_ford.py) **- O(mn)**
1717
* :small_red_triangle: [Floyd-Warshall Shortest Path](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/graphtheory/floyd-warshall/floyd-warshall.py) **- O(n<sup>3</sup>)**
18-
* :white_check_mark: [Dijkstra Shortest Path](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/graphtheory/dijkstra/djikstra.py) **- Naive implementation**
19-
* :white_check_mark: [Dijkstra Shortest Path](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/graphtheory/dijkstra/heapdijkstra.py) **O(mlog(n)) - Heap implementation**
18+
* :white_check_mark: [Dijkstra Shortest Path](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/graphtheory/dijkstra/dijkstra.py) **- Naive implementation**
19+
* :white_check_mark: [Dijkstra Shortest Path](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/graphtheory/dijkstra/heapdijkstra.py) **- O(mlog(n)) - Heap implementation**
2020
* :small_red_triangle: [Karger's Minimum cut](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/graphtheory/kargers/kargermincut.py)
21-
* :small_red_triangle: [Prim's Algorithm](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/graphtheory/prims/prims_algorithm.py) **- O(mn) Naive implementation**
22-
* :white_check_mark: [Prim's Algorithm](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/graphtheory/prims/primheap.py) **- O(mlog(n)) Heap implementation**
21+
* :small_red_triangle: [Prim's Algorithm](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/graphtheory/prims/prim_naive.py) **- O(mn) Naive implementation**
22+
* :white_check_mark: [Prim's Algorithm](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/graphtheory/prims/prim_heap.py) **- O(mlog(n)) Heap implementation**
2323
* :small_red_triangle: [Kruskal's Algorithm](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/graphtheory/kruskal/kruskal.py) **- O(mn) implementation**
2424
* :white_check_mark: [Kruskal's Algorithm](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/graphtheory/kruskal/kruskal_unionfind.py) **- O(mlog(n))**
2525
* :white_check_mark: [Breadth First Search](https://github.com/AladdinPerzon/Algorithms-Collection-Python/blob/master/Algorithms/graphtheory/breadth-first-search/BFS_queue_iterative.py) **- O(n + m) - Queue Implementation**

0 commit comments

Comments
 (0)