def buildFromList(L):
    """
    L: for each task t, a pair (duration, list of tasks)
    - duration: its duration (a float) !
    - list of tasks: that have to be finished before t 
    
    returns:
        G (Graph): the digraph with last two vertices added as vitual tasks 
            task n is the beginning of the project
            task n+1 is the end
        G_1 (Graph): G reversed!
        din, dout: vectors of in- and out- degrees in G
    """

    n = len(L)

    G = graph.Graph(n + 2, directed=True)
    G.costs = {}
    G_1 = graph.Graph(n + 2, directed=True)
    G_1.costs = {}

    dout = [0] * (n + 2)
    din = [0] * (n + 2)

    for y in range(n):
        din[y] = len(L[y][1])  # L[y][1]: y's predecessors
        for x in L[y][1]:
            G.addedge(x, y)
            G.costs[(x, y)] = L[x][0]
            dout[x] += 1
            G_1.addedge(y, x)
            G_1.costs[(y, x)] = L[x][0]

    for x in range(n):
        if din[x] == 0:
            G.addedge(n, x)  # add edges from the first task n to sources
            G_1.addedge(x, n)
            G.costs[(n, x)] = G_1.costs[(x, n)] = 0
            dout[n] += 1
            din[x] = 1
        if dout[x] == 0:
            G.addedge(x, n + 1)  # add edges from sinks to the last task (n+1)
            G_1.addedge(n + 1, x)
            G.costs[(x, n + 1)] = G_1.costs[(n + 1, x)] = L[x][0]
            dout[x] = 1
            din[n + 1] += 1

    return (G, G_1, din, dout)
def buildgraph(filename, k):
    """Build a graph with words of length k from a lexicon

    """

    file = open(filename, "r")
    lines = file.readlines()
    labels = []
    for word in lines:
        mot = word.strip()
        if(len(mot)==k):
            labels.append(mot)
    res = graph.Graph(len(labels), False, labels)

    for i in range(len(labels)):
        for j in range(i+1, len(labels)):
            size = 0
            diff = 0
            while(diff<2 and size<k):
                if(labels[i][size]!=label[j][size]):
                    diff+=1
                size+=1
            if(diff==1):
                res.addedge(i, j)
    file.close()
    return res
def condenseGraph(G):
    k, scc = Tarjan(G)
    scc = [i-1 for i in scc]
    
    Gr = graph.Graph(k, True)
    for s in range(G.order):
        for adj in G.adjlists[s]:
            if scc[s] != scc[adj] and not scc[adj] in Gr.adjlists[scc[s]]:
                Gr.addedge(scc[s], scc[adj])
    return (Gr, scc)
Example #4
0
def makeMeToutPiti(G):
	""" Returns:
	Graph: le graphe réduit
	Graph: le graphe inverse du graphe réduit
	int list: le vecteur qui à un noeud du graphe d'origine associe sa composante fo-connexe
	int list: le vecteur qui à une composante fo-connexe associe un noeud qui la contient
	"""
	ccf, k, res2 = tarjan_algorithm(G)
	Gr = graph.Graph(k, directed = True)
	GrI = graph.Graph(k, directed = True)

	for i in range(G.order):
		k1 = ccf[i]
		l = Gr.adjlists[k1]

		for j in G.adjlists[i]:
			k2 = ccf[j]
			if k1 != k2 and k2 not in l:
				Gr.addedge(k1, k2)
				GrI.addedge(k2, k1)

	return Gr, GrI, ccf, res2
Example #5
0
    def run(self):
        for i in range(self.G.order):
            if not self.p[i]:
                self.__rec(i)

        mp = [None] * self.ccfid
        c2n, j = [], 0

        self.Gr = graph.Graph(0, True)
        self.GrI = graph.Graph(0, True)

        src, sin = [], []

        for i in range(self.ccfid):
            if not self.c2s[i]:
                continue

            src.append(j)
            mp[i], j = j, j + 1
            c2n.append(self.c2n[i])

            self.Gr.addvertex()
            self.GrI.addvertex()

            for c in self.c2r[i]:
                if not mp[c]:
                    sin.append(j)
                    mp[c], j = j, j + 1
                    c2n.append(self.c2n[c])
                    self.Gr.addvertex()
                    self.GrI.addvertex()

                self.Gr.addedge(mp[i], mp[c])
                self.GrI.addedge(mp[c], mp[i])

        self.c2n = c2n
        return src, sin
Example #6
0
def butterGraph(T, A, C):
    G = graph.Graph(7, True)
    G.costs = {}
#    G.labels = ["PB", "B", "A", "S", "I", "F", "E"]
    for i in range(7):
        for j in range(7):
            if T[i][j] or C[i][j] or A[i][j]:
                val = T[i][j] + C[i][j] - A[i][j]
                G.addedge(i, j)
                G.costs[(i, j)] = val
    return G

#TODO: reverse vertices
    
    
    
def Kruskal(G):
    H = []
    for x in range(G.order):
        for y in G.adjlists[x]:
            if x > y:
                heapq.heappush(H, (G.costs[(x, y)], x, y))

    p = [-1] * G.order  # to use with union2
    T = graph.Graph(G.order, directed=False, costs=True)
    n = 0
    while n < G.order - 1 and H:
        (c, x, y) = heapq.heappop(H)
        if union2(x, y, p):
            T.addedge(x, y, c)
            n = n + 1
    if n < G.order - 1:
        raise Exception("Not connected")
    return T
def Prim(G, x=0):
    T = graph.Graph(G.order, False, costs=True)
    d = [inf] * G.order
    p = [-1] * G.order
    M = [False] * G.order  # vertices already in solution
    H = heap.Heap(G.order)
    d[x] = 0  #useless
    M[x] = True
    n = 0
    while n < G.order - 1:
        for y in G.adjlists[x]:
            if not M[y] and G.costs[(x, y)] < d[y]:
                d[y] = G.costs[(x, y)]
                p[y] = x
                H.update(y, d[y])

        if H.isEmpty():
            raise Exception("Not connected")
        (_, x) = H.pop()
        M[x] = True
        T.addedge(x, p[x], d[x])
        n += 1
    return T
def reverseGraph(G):
    G_1 = graph.Graph(G.order, True)
    for s in range(G.order):
        for adj in G.adjlists[s]:
            G_1.addedge(adj, s)
    return G_1
# -*- coding: utf-8 -*-
"""
Undergraduates - S3
Examples of graphs (from tutorial)

"""

from algopy import graph, graphmat

G1 = graph.Graph(9, True)
G1.adjlists = [[1, 1, 1, 6, 2], [3, 3], [6, 8], [6], [3], [2, 6], [3, 4],
               [6, 5, 8], [8]]

G1mat = graphmat.GraphMat(9, True)
G1mat.adj = [[0, 3, 1, 0, 0, 0, 1, 0, 0], [0, 0, 0, 2, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 1, 0, 0],
             [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 1, 0, 0],
             [0, 0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 0, 1],
             [0, 0, 0, 0, 0, 0, 0, 0, 1]]

G2 = graph.Graph(9, False)
G2.adjlists = [[2, 1, 1, 1], [0, 0, 0, 3, 3], [0], [1, 1], [5, 6, 7],
               [4, 7, 8], [4, 7], [4, 6, 5, 8, 7], [7, 5]]

G2mat = graphmat.GraphMat(9, False)
G2mat.adj = [[0, 3, 1, 0, 0, 0, 0, 0, 0], [3, 0, 0, 2, 0, 0, 0, 0, 0],
             [1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 2, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 1, 1, 1, 0], [0, 0, 0, 0, 1, 0, 0, 1, 1],
             [0, 0, 0, 0, 1, 0, 0, 1, 0], [0, 0, 0, 0, 1, 1, 1, 1, 1],
             [0, 0, 0, 0, 0, 1, 0, 1, 0]]