Skip to content

Commit e38d91f

Browse files
committed
add combinatorics example
1 parent 30db21e commit e38d91f

7 files changed

Lines changed: 82 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python3
2+
# encoding: utf-8
3+
from itertools import *
4+
5+
def permutation(s):
6+
for item in permutations(s):
7+
if item:
8+
print(''.join(i for i in item), end=' ') # abc acb bac bca cab cba
9+
print('')
10+
11+
def permutation2(s, i):
12+
for item in permutations(s, i):
13+
if item:
14+
print(''.join(i for i in item), end=' ') # abc acb bac bca cab cba
15+
print('')
16+
17+
def permutation3(s, i):
18+
for item in product(s, repeat=i):
19+
if item:
20+
print(''.join(i for i in item), end=' ') # abc acb bac bca cab cba
21+
print('')
22+
23+
def combination(s, i):
24+
for item in combinations(s, i):
25+
if item:
26+
print(''.join(i for i in item), end=' ') # abc acb bac bca cab cba
27+
print('')
28+
29+
def combination2(s, i):
30+
for item in combinations_with_replacement(s, i):
31+
if item:
32+
print(''.join(i for i in item), end=' ') # abc acb bac bca cab cba
33+
print('')
34+
35+
if __name__ == '__main__':
36+
permutation('abc')
37+
permutation('abb')
38+
permutation2('abc', 2)
39+
permutation3('abc', 2)
40+
combination('abcd', 2)
41+
combination2('abcd', 2)

‎graph/dijkstra_shortest_path.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
#!/usr/bin/env python3
3+
# encoding: utf-8
4+
#
5+
# Copyright (c) 2017 Vladimir Shurygin. All rights reserved.
6+
#
7+
'''
8+
Dijkstra algorithm find shortest path in graph Shortest paths in graphs without negative edges.
9+
'''
10+
#add parent directory with base module
11+
import os
12+
from sys import path
13+
path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../bst')))
14+
15+
from undirected_cyclic import Graph
16+
from vertex import Vertex
17+
from bst_heap_min import MinHeap
18+
19+
def dijkstra_min_priority_queue(graph, start, finish):
20+
''' Implementation with min-priority queue
21+
BigO = V**2, (where |V| is the number of nodes).
22+
'''
23+
if not isinstance(graph, Graph):
24+
return None
25+
if start is None:
26+
return None
27+
heap = MinHeap()
28+
visited = {}
29+
heap.push({'vertex':start.id, 'value':0})
30+
path = {}
31+
while heap.array:
32+
obj = heap.pop()
33+
vertex = graph.get_vertex(obj['vertex'])
34+
if vertex.id in visited:
35+
continue
36+
for adj_vertex_id in vertex.adj:
37+
adj_vertex = vertex.adj[adj_vertex_id]
38+
edge_property = vertex.get_edge_property(adj_vertex_id)
39+
heap.push({'vertex':adj_vertex, 'value':None})
40+
41+
pass

‎graph/find_path.py‎

100644100755
File mode changed.

‎hashing/test_div_basket.py‎

100644100755
File mode changed.

‎hashing/test_multi_basket.py‎

100644100755
File mode changed.

‎hashing/test_rolling_hash.py‎

100644100755
File mode changed.

‎hashing/test_utility.py‎

100644100755
File mode changed.

0 commit comments

Comments
 (0)