Exemple #1
0
def task_three():
    GRAPH_FILE_PATH = "F:\GitHub\GiSwI\_model_\_files\_lists_\w.txt"
    GRAPH_FILE_MODE = 'r'
    GRAPH_FILE = _file_.readWriteFile(GRAPH_FILE_PATH, GRAPH_FILE_MODE)
    FILE_DESCRIPTION = "Plik uzyty do wczytania grafu: " + str(GRAPH_FILE.name)
    GRAPH_DICTIONARY = _file_.convertGraphFileIntoDictionaryGraphvizStyle(
        GRAPH_FILE)
    GRAPH = _graph_.Graph(GRAPH_DICTIONARY, 2)
    GRAPH_EDGE_SEPARATOR = GRAPH.GRAPH_EDGE_SEPARATOR

    result = ""
    _description = []

    build_array(_description, FILE_DESCRIPTION)
    build_array(_description, "\n")

    edges = edges_dictionary(GRAPH.getEdgesCollection(),
                             GRAPH.getUniqueVertices())

    build_array(
        _description,
        "\nWierzcholki i krawedzie, majace polaczenie z wszystkimi innymi wierzcholkami: "
    )
    allFullEdges = {}
    for i in edges:
        count = 0

        if len(edges.get(i)) == (len(GRAPH.getUniqueVertices()) - 1):
            for j in GRAPH.getUniqueVertices():
                if int(j) in edges.get(i):
                    count += 1

            if count == (len(GRAPH.getUniqueVertices()) - 1):
                allFullEdges[i] = edges.get(i)
                build_array(_description, "\nWierzcholek: ")
                build_array(_description, str(i))
                build_array(_description, ", Krawedzie: ")
                build_array(_description, str(allFullEdges[i]))

    maxVerticeToRemove = max(allFullEdges)
    del edges[maxVerticeToRemove]
    for i in edges.values():
        i.remove(maxVerticeToRemove)

    build_array(_description, "\n\nWybrano do usuniecia wierzcholek: ")
    build_array(_description, str(maxVerticeToRemove))
    build_array(_description, " gdyz jest najwiekszy.")

    newCollection = []
    for i in GRAPH.getEdgesCollection():
        first = int(i[0])
        second = int(i[-1])

        if (first != maxVerticeToRemove) and (second != maxVerticeToRemove):
            newCollection.append(i)

    newUniqueVertices = []
    for i in newCollection:
        s = i.split(GRAPH_EDGE_SEPARATOR)

        first = s[0]
        second = s[1]

        if first not in newUniqueVertices:
            newUniqueVertices.append(first)
        if second not in newUniqueVertices:
            newUniqueVertices.append(second)

    cycliGraphName = "c"
    cyclicGraph = createCyclicGraph(newCollection, newUniqueVertices,
                                    cycliGraphName)
    createCyclicGraphFile(cyclicGraph)

    build_array(
        _description,
        "\nPo usunieciu wierzcholka, zostal utworzony na jego podstawie graf cykliczny "
    )
    build_array(_description, cycliGraphName.upper())
    build_array(_description, " o krawedziach: \n")
    for i in newCollection:
        build_array(_description, str(i))
        build_array(_description, "\n")

    result = result.join(_description)

    return result
'''
Created on 27.05.2017

@author: Mateusz Mucha
'''
from _model_._files import file as _file_
from _model_._graph import graph as _graph_

GRAPH_FILE_PATH = "F:\GitHub\GiSwI\_model_\_files\_lists_\graph_file_task_1_v1.txt"
GRAPH_FILE_MODE = 'r'
GRAPH_FILE = _file_.readWriteFile(GRAPH_FILE_PATH, GRAPH_FILE_MODE)
FILE_DESCRIPTION = "Plik uzyty do wczytania grafu: " + str(GRAPH_FILE.name)
GRAPH_DICTIONARY = _file_.convertGraphFileIntoDictionary(GRAPH_FILE)
GRAPH = _graph_.Graph(GRAPH_DICTIONARY, 1)
GRAPH_EDGE_SEPARATOR = GRAPH.GRAPH_EDGE_SEPARATOR
'''
Help methods.
'''


def comma_string(collection_):
    buildedString_ = ", ".join(collection_)

    return buildedString_


def replacing(value, replaceType):
    if replaceType == '1':
        return str(value) \
                .replace("[", "| ") \
                .replace("]", " |") \
def task_five(): 
    GRAPH_FILE_PATH = "F:\GitHub\GiSwI\_model_\_files\_lists_\graf_pelny.txt"
    GRAPH_FILE_MODE = 'r'
    GRAPH_FILE = _file_.readWriteFile(GRAPH_FILE_PATH, GRAPH_FILE_MODE)
    FILE_DESCRIPTION = "Plik uzyty do wczytania grafu: " + str(GRAPH_FILE.name)
    GRAPH_DICTIONARY = _file_.convertGraphFileIntoDictionary(GRAPH_FILE)
    GRAPH = _graph_.Graph(GRAPH_DICTIONARY, 3)
    GRAPH_EDGE_SEPARATOR = GRAPH.GRAPH_EDGE_SEPARATOR
    
    result = ""    
    _description = []
    
    print(FILE_DESCRIPTION + "\n")
    city = str(input("Podaj wierzcholek[1 do 5], z ktorego i do ktorego chcesz znalezc sciezke: "))
    
    edgesCollection = GRAPH.getEdgesCollection()
    weightDictionary = GRAPH.getGraphWeights()
    
    graph = nx.Graph()
    for i in weightDictionary:
        s = i.split()
        v1 = s[0]
        v2 = s[2]
        weight = weightDictionary.get(i)
        graph.add_edge(v1, v2, weight=float(weight)) 
        
    
    nodes = graph.nodes() 
    nodes.remove(city)  
    lowest = 0   
    biggest = 0
    equaling = 0  
    pathsDict = {}
    
    for i in nodes: 
        for j in nx.all_simple_paths(graph, source=city, target=i):
            if len(j) > len(nodes):
                way = 0
                counter = 1
                
                while(counter < len(j)):
                    way += graph[j[counter - 1]][j[counter]]['weight']
                    counter += 1
                way += graph[j[-1]][city]['weight']
                j.append(city)
                
                if equaling < 1:
                    lowest = way
                    biggest = way
                    equaling += 1
                elif way < lowest:
                    lowest = way
                elif way > biggest:
                    biggest = way
                
                pathsDict[way] = j
    
    build_array(_description, "\n\nNajkrotsza droga dla miasta ")
    build_array(_description, str(city))
    build_array(_description, " to: \n")
    count = 0
    for i in pathsDict.get(lowest):
        build_array(_description, str(i))
        count += 1
        if count < len(pathsDict.get(lowest)):
            build_array(_description, str(" -> "))
    
    build_array(_description, str("\nI wynosi: "))
    build_array(_description, str("{0:.2f}".format(round(lowest, 2))))
    
    build_array(_description, "\n\nNajdluzsza droga dla miasta ")
    build_array(_description, str(city))
    build_array(_description, " to: \n")
    count = 0
    for i in pathsDict.get(biggest):
        build_array(_description, str(i))
        count += 1
        if count < len(pathsDict.get(biggest)):
            build_array(_description, str(" -> "))
    
    build_array(_description, str("\nI wynosi: "))
    build_array(_description, str("{0:.2f}".format(round(biggest, 2))))
    
    
    result = result.join(_description)
    
    createFile = open("F:\GitHub\GiSwI\_model_\_files\_lists_\graf_pelny_paths.txt", 'w')
    for i in sorted(pathsDict):
        createFile.write(str("{0:.2f}".format(round(i, 2))))
        createFile.write(str(" :: "))
        createFile.write(str(pathsDict.get(i)))
        createFile.write(str("\n"))
    createFile.close() 
    
    
    return result