Пример #1
0
    def RandomNetwork(class_object, V, E):
        tmp = class_object(V)
        if (E < 0):
            raise Exception("Number of edges must be nonnegative")

        for i in range(E):
            v = random.randint(0, V - 1)
            w = random.randint(0, V - 1)
            capacity = random.randint(0, 99)
            tmpE = FlowEdge(v, w, capacity)
            tmp.addEdge(tmpE)

        return tmp
    def isNontrivial(self, team):
        self.cert = []
        size = 2 + self.teamNum + (self.teamNum - 1) * (self.teamNum - 2) // 2
        network = FlowNetwork(size)
        sink = size - 1
        source = sink - 1
        teamIndex = self._teams[team]
        current = self._wins[teamIndex] + self._remain[teamIndex]
        maxFlow = 0
        # connect team node to sink node
        for i in range(self.teamNum):
            if i != teamIndex:
                network.addEdge(FlowEdge(i, sink, current - self._wins[i]))
        # game nodes index start from teamNum
        gameNode = self.teamNum
        for i in range(self.teamNum):
            if i != teamIndex:
                for j in range(i + 1, self.teamNum):
                    if j != teamIndex and int(self.againstList[i][j]) != 0:
                        # connect scource node to game nodes
                        network.addEdge(
                            FlowEdge(source, gameNode,
                                     int(self.againstList[i][j])))
                        maxFlow += int(self.againstList[i][j])
                        # connect game nodes to team nodes

                        network.addEdge(FlowEdge(gameNode, i, 1000000000))
                        network.addEdge(FlowEdge(gameNode, j, 1000000000))
                        gameNode += 1

        # compute maxflow
        maxflow = FordFulkerson(network, source, sink)
        if maxflow.value() == maxFlow:
            return False
        # store min cut as certificate
        for team in self._teams.keys():
            if (maxflow.inCut(self._teams[team])):
                self.cert.append(team)
        return True
Пример #3
0
from FordFulkerson import FordFulkerson
from FlowNetwork import FlowNetwork
from FlowEdge import FlowEdge

#V = 10
#E = 100
#G = FlowNetwork.RandomNetwork(V,E)
G = FlowNetwork(6)
G.addEdge(FlowEdge(0, 1, 2.0))
G.addEdge(FlowEdge(0, 2, 3.0))
G.addEdge(FlowEdge(1, 3, 3.0))
G.addEdge(FlowEdge(1, 4, 1.0))
G.addEdge(FlowEdge(2, 3, 1.0))
G.addEdge(FlowEdge(2, 4, 1.0))
G.addEdge(FlowEdge(3, 5, 2.0))
G.addEdge(FlowEdge(4, 5, 3.0))

s = 0
t = G.V() - 1

print(G)

# compute maximum flow and minimum cut
maxflow = FordFulkerson(G, s, t)
print("Max flow from", s, "to", t)

for v in range(G.V()):
    for e in G.adj(v):
        if ((v == e.From()) and e.flow() > 0):
            print(" ", e)
Пример #4
0
    def graph_construction(self, team: str):

        self.graph = dict()
        graph = self.graph
        self.cur_team = team
        self.temp_teams = copy.deepcopy(self.indexToTeam)
        temp_teams = self.temp_teams
        temp_teams.pop(self.teamToIndex[team])

        mix = self.wins(team) + self.remaining(team)

        c = self.teams - 1

        graph['src'] = dict()
        graph['dest'] = dict()

        for x in temp_teams:
            graph[x] = dict()
            forward = FlowEdge(max(0, mix - self.wins(x)))
            if mix - self.wins(x) < 0:
                self.results[self.cur_team] = True
                self.certificates[self.cur_team] = {x}
                return
            reverse = FlowEdge(0)
            forward.connect(reverse)
            reverse.connect(forward)
            graph[x]['dest'] = forward
            graph['dest'][x] = reverse

        for i in range(c):
            for j in range(i + 1, c):
                node = temp_teams[i] + " || " + temp_teams[j]
                graph[node] = dict()

                balance = self.against(temp_teams[i], temp_teams[j])
                forward1 = FlowEdge(balance)
                reverse1 = FlowEdge(0)
                forward1.connect(reverse1)
                reverse1.connect(forward1)
                graph['src'][node] = forward1
                graph[node]['src'] = reverse1

                forward2 = FlowEdge(100000)
                reverse2 = FlowEdge(0)
                forward2.connect(reverse2)
                reverse2.connect(forward2)
                graph[node][temp_teams[i]] = forward2
                graph[temp_teams[i]][node] = reverse2

                forward3 = FlowEdge(100000)
                reverse3 = FlowEdge(0)
                forward3.connect(reverse3)
                reverse3.connect(forward3)
                graph[node][temp_teams[j]] = forward3
                graph[temp_teams[j]][node] = reverse3

        self.ford_fulkerson()
    def isEliminated(self, team):
        check = self.trivial(
            team
        )  #to check whether the team is eliminated by trivial case or not
        if (check):
            return self.winningteams
        else:
            n = len(self.otherteams)  #get the length of the list
            graphmap = {}  #initializing map for all vertices
            pos = 1
            s = 0
            graphmap[s] = 0  #s taking s as 0
            #taking each vertex pairs and mapped
            for i in range(n):
                for j in range(i, n):
                    if i != j:
                        graphvertices = (self.otherteams[i],
                                         self.otherteams[j])
                        graphmap[graphvertices] = pos
                        pos = pos + 1
            temp = pos  #taking this value to cut the game vertices
            #adding the team names to the map
            for i in range(n):
                graphmap[self.otherteams[i]] = pos
                pos = pos + 1
            graphmap[pos] = pos  #adding t value as the final one to the map
            num = int((n * (n - 1)) / 2)
            v = len(graphmap)  # length of the vertices to
            G = FlowNetwork(v)
            t = G.V() - 1
            totalwin = self.wins(team) + self.remaining(
                team)  #total wins a team can win
            #adding the game vertices to the graph
            for i in range(n):
                for j in range(i, n):
                    if i != j:
                        #print(i,j,n)
                        a = self.otherteams[i]
                        b = self.otherteams[j]
                        rem = self.against(a, b)
                        pair = (a, b)
                        vertex = graphmap[pair]
                        G.addEdge(FlowEdge(graphmap[s], vertex, rem))
                        G.addEdge(FlowEdge(vertex, graphmap[a], inf))
                        G.addEdge(FlowEdge(vertex, graphmap[b], inf))
            #print(G)
            #adding team vertices to graph
            for i in range(n):
                a = self.otherteams[i]
                win = self.wins(a)
                tot = totalwin - win
                G.addEdge(FlowEdge(graphmap[a], graphmap[pos], tot))

            #print(G)
            maxflow = FordFulkerson(G, s, t)
            #print("Max flow from",s,"to",t)

            for v in range(G.V()):
                for e in G.adj(v):
                    if ((v == e.From()) and e.flow() > 0):
                        pass
                        #print(" ",e)
            cut = []
            for v in range(temp, G.V()):
                if (maxflow.inCut(v)):
                    cut.append(v)
            #print(graphmap)
            #print("Min cut:",cut)
            #print("Max flow value =",maxflow.value())
            self.subset = []
            if (len(cut) > 0):
                for cutval in cut:
                    for key, val in graphmap.items():
                        if cutval == val:
                            #print(key)
                            self.subset.append(key)
                return self.subset
            else:
                return False
Пример #6
0
    def isEliminated(self, team):
        table1 = {}
        table2 = {}
        val = 1
        count1 = 0
        count2 = 0
        while count1 < len(self.data) - 1:
            count2 = count1 + 1
            while count2 < len(self.data):
                table1[str(count1) + "-" + str(count2)] = val
                val = val + 1
                count2 = count2 + 1
            count1 = count1 + 1

        val2 = val
        self.reference = val2
        count1 = 0
        while count1 < len(self.data):
            table2[count1] = val
            val = val + 1
            count1 = count1 + 1
        G = FlowNetwork(1 + len(table1) + len(table2) + 1)
        for x in table1:
            t = str(x).split('-')
            G.addEdge(
                FlowEdge(
                    0, int(table1[x]),
                    int(
                        self.against(self.data[int(t[0])]["teamName"],
                                     self.data[int(t[1])]["teamName"]))))
            G.addEdge(
                FlowEdge(int(table1[x]), int(table2[int(t[0])]), float("inf")))
            G.addEdge(
                FlowEdge(int(table1[x]), int(table2[int(t[1])]), float("inf")))

        #
        # print(val)
        self.flag = False
        for x in table2:
            q = int(self.wins(team))
            w = int(self.remaining(team))
            e = int(self.wins(self.data[int(x)]["teamName"]))
            if (q + w < e):
                self.flag = True
                self.finalResult.append(self.data[int(x)]["teamName"])
                # G.addEdge(FlowEdge(int(table2[x]),val,0))
            elif not self.flag:
                G.addEdge(FlowEdge(int(table2[x]), val, int(q + w - e)))

        s = 0
        t = G.V() - 1

        if not self.flag:
            maxflow = FordFulkerson(G, s, t)
            # print("Max flow from",s,"to",t)

            # print min-cut
            cut = []
            for v in range(G.V()):
                if (maxflow.inCut(v)):
                    cut.append(v)
            # print("Min cut:",cut)
            # print("Max flow value =",maxflow.value())
            self.teams1Send = table1
            self.teams2Send = table2
            if len(cut) == 1:
                self.result = False
                return False
            else:
                self.result = True
                self.resultArr = cut
                return True
        return True