コード例 #1
0
ファイル: test_dag.py プロジェクト: giltayar/google-ex
    def test_djikstra(self):
        dag = Dag()
        a, b, c, d = dag.add_vertices(4)
        dag.add_edge(a, b, 3)
        dag.add_edge(a, c, 4)
        dag.add_edge(b, d, 4)
        dag.add_edge(c, d, 1)

        self.assertEqual(dag.djikstra(a, d), (5, [a, c, d]))
コード例 #2
0
ファイル: test_dag.py プロジェクト: giltayar/google-ex
    def test_difficult_dag(self):
        dag = Dag()

        v = dag.add_vertices(10)

        dag.add_edge(v[0], v[1], 1)
        dag.add_edge(v[0], v[2], 2)
        dag.add_edge(v[1], v[2], 5)
        dag.add_edge(v[1], v[3], 1)
        dag.add_edge(v[2], v[4], 2)
        dag.add_edge(v[3], v[2], 3)
        dag.add_edge(v[3], v[4], 1)
        dag.add_edge(v[4], v[5], 2)

        self.assertEqual(dag.djikstra(v[0], v[5]), (5, [v[0], v[1], v[3], v[4], v[5]]))

        dag.add_edge(v[2], v[5], 1)
        self.assertEqual(dag.djikstra(v[0], v[5]), (3, [v[0], v[2], v[5]]))
コード例 #3
0
ファイル: dfs.py プロジェクト: danieljsharpe/py_primer
import random
from dag import Dag

#Create a random graph
n_nodes = 10
n_edges = 20
randseed = 18
graph_is_dag = True

dag1 = Dag()
random.seed(randseed)

for i in range(n_nodes):
    dag1.add_node(i)
for i in range(n_edges):
    node1 = random.randint(0,n_nodes-1)
    node2 = random.randint(0,n_nodes-1)
    dag1.add_edge(node1, node2)
if graph_is_dag:
    while not dag1.validate():
        node1 = random.randint(0,n_nodes-1)
        node2 = random.randint(0,n_nodes-1)
        dag1.add_edge(node1, node2)
        # remove an edge between two nodes that are known to be connected
        print dag1.graph
print dag1.graph
コード例 #4
0
from dag import Dag
from random import randrange
import numpy as np


#Construct a random DAG
n = 10 # no. of nodes
p = 0.2 # probability of adding an edge
m = 50 # max. no. of edge-adding attempts
dag1 = Dag()
for i in range(n):
    dag1.add_node(i)
for i in range(m):
    addedge = np.random.choice(a=[True, False], p=[p,1-p])
    if addedge:
        j = randrange(0,n-2)
        k = j
        while k <= j: 
            k = randrange(0,n-1)
        dag1.add_edge(j,k)
    else:
        continue
#if there are any independent nodes (other than the first), add a dependency
while dag1.ind_nodes() != [0]:
    i = dag1.ind_nodes()[-1]
    if i != 1: dag1.add_edge(randrange(0,i-1),i)
    elif i == 1: dag1.add_edge(0,i)

print dag1.graph