def req4(catalog): grf=catalog['connections'] prim=pm.PrimMST(grf) weight=pm.weightMST(grf,prim) print('La distancia total de la red de expansion minima es de '+str(round(weight,2))+' km.') edges = pm.edgesMST(grf,prim) print('El tamano de la red de expansion minima es de '+str(edges['mst']['size'])+' landing points.')
def requerimiento4(catalog): pri = prim.PrimMST(catalog['connections']) peso = prim.weightMST(catalog['connections'], pri) mst = prim.edgesMST(catalog['connections'], pri)['mst'] m = folium.Map(location=[4.6, -74.083333], tiles="Stamen Terrain") for st in lt.iterator(mst): cv = st['vertexA'].split("-", 1) ce = st['vertexB'].split("-", 1) infov = mp.get(catalog['points'], cv[0])['value'] infoe = mp.get(catalog['points'], ce[0])['value'] addPointConneMst(catalog, st['vertexA'], st['vertexB'], st['weight']) folium.PolyLine(locations=[(float(infov['latitude']), float(infov['longitude'])), (float(infoe['latitude']), float(infoe['longitude']))], tooltip=str(cv[1])).add_to(m) folium.Marker([float(infov['latitude']), float(infov['longitude'])], popup=str(infov['name'])).add_to(m) folium.Marker([float(infoe['latitude']), float(infoe['longitude'])], popup=str(infoe['name'])).add_to(m) m.save('mapa_req4.html') gramst = catalog['mst'] vert = gr.vertices(gramst) num = lt.size(vert) primero = lt.firstElement(vert) mayor = 0 camino = None dijta = djk.Dijkstra(catalog['mst'], primero) for v in lt.iterator(vert): ruta = djk.pathTo(dijta, v) x = lt.size(ruta) if x > mayor: mayor = x camino = ruta return num, peso, camino
def minimumSpanningTree(ana): countries = ana['countries'] countryKeys = m.keySet(countries) points = ana['points'] search = prim.PrimMST(ana['connections']) relaxed = prim.prim(ana['connections'], search, 'Bogota') prim.edgesMST(ana['connections'], search) mst = relaxed['mst'] size = lt.size(mst) weight = round(prim.weightMST(ana['connections'], relaxed), 2) M = folium.Map() for i in range(0, lt.size(mst)): camino = lt.getElement(mst, i) punto = camino['vertexB'] code = punto.split('*')[0] try: int(code) lPoint = me.getValue(m.get(points, code)) pointLat = lPoint['latitude'] pointLon = lPoint['longitude'] pointLoc = pointLat, pointLon name = str(i) + '. ' + lPoint['name'] folium.Marker(pointLoc, name, icon=folium.Icon(color='blue')).add_to(M) except: capital = code n = 0 found = False while n < lt.size(countryKeys) and found == False: key = lt.getElement(countryKeys, n + 1) info = me.getValue(m.get(countries, key)) Capital = info['CapitalName'] if capital == Capital: found = True country = info['CountryName'] capLat = info['CapitalLatitude'] capLon = info['CapitalLongitude'] capLoc = capLat, capLon capName = str(i) + '. ' + country + ', ' + capital folium.Marker(capLoc, capName, icon=folium.Icon(color='pink')).add_to(M) n += 1 M.save('MST.html') return weight, size
def expansionMin(macrostructure): mst = prm.PrimMST(macrostructure['connections']) camino = prm.edgesMST(macrostructure['connections'], mst) numero = mp.size(camino['edgeTo']) peso_total = 0 keys = mp.keySet(camino['distTo']) for index in range(1, int(lt.size(keys)) + 1): data = lt.getElement(keys, index) peso = mp.get(camino['distTo'], data)['value'] peso_total += int(peso) return numero, peso_total
def findGraphMST(catalog): """Usa Prim para crear el MST""" mst_structure = prim.PrimMST(catalog['internet_graph']) mst_structure = prim.edgesMST(catalog['internet_graph'], mst_structure) mst = mst_structure['mst'] edgesTo = mst_structure['edgeTo'] mst_weight = prim.weightMST(catalog['internet_graph'], mst_structure) nodes = lt.size(mst) mst_graph = createMSTgraph(catalog, mst) path = getMSTroots(catalog, mst_graph) return nodes, mst_weight, path
def criticalInfrastructure(analyzer): vertex = gr.numVertices(analyzer["connections"]) tree = pr.PrimMST(analyzer["connections"]) weight = pr.weightMST(analyzer["connections"], tree) branch = pr.edgesMST(analyzer["connections"], tree) branch = branch["edgeTo"]["table"]["elements"] max = 0 for i in range(len(branch)): value = branch[i]["value"] if (value != None) and (float(value["weight"]) > max): max = value["weight"] return vertex, weight, max
def req4(analyzer): vertex= gr.numVertices(analyzer['connections']) arbol= pr.PrimMST(analyzer['connections']) weight= pr.weightMST(analyzer['connections'],arbol) branch= pr.edgesMST(analyzer['connections'],arbol) branch= branch['edgeTo']['table']['elements'] maximo=0 for a in range(len(branch)): valor= branch[a]['value'] if (valor != None) and (float(value['weight'])> max): maximo = value['weight'] print("Numero de nodos conectados a la red de expansión mínima: "+ str(vertex)) print("Costo total de la red de expansión mínima: "+ str(weight)) print("Rama más larga que hace parte de la red de expansión mínima: "+str(maximo)) return vertex,weight,maximo
def infraestructura_critica(analyzer): arbol = p.PrimMST(analyzer["Arcos"]) vertices = gr.numVertices(analyzer["Arcos"]) Peso = p.weightMST(analyzer["Arcos"], arbol) rama = p.edgesMST(analyzer["Arcos"], arbol) rama = rama["edgeTo"]["table"]["elements"] maximo = 0 for i in range(len(rama)): valor = rama[i]["value"] if (valor is not None) and (float(valor["weight"]) > maximo): maximo = valor["weight"] vertice1 = valor["vertexA"] vertice2 = valor["vertexB"] cables = mp.valueSet(analyzer["cables_origen"]) iterador = it.newIterator(cables) while it.hasNext(iterador): elemento = it.next(iterador) if elemento["origin"] == vertice1 and vertice2 == elemento[ "destination"]: cable = elemento["cable_name"] return vertices, Peso, cable, maximo