def compute_features(self): # checking if eulerian self.add_feature( "eulerian", lambda graph: nx.is_eulerian(graph) * 1, "A graph is eulerian if it has a eulerian circuit: a closed walk that includes \ each edges of the graph exactly once", InterpretabilityScore(3), ) # checking if semi eulerian self.add_feature( "semi_eulerian", lambda graph: nx.is_semieulerian(graph) * 1, "A graph is semi eulerian if it has a eulerian path but no eulerian circuit", InterpretabilityScore(3), ) # checking if eulerian path exists self.add_feature( "semi_eulerian", lambda graph: nx.has_eulerian_path(graph) * 1, "Whether a eulerian path exists in the network", InterpretabilityScore(3), )
def test_is_semieulerian(self): # Test graphs with Eulerian paths but no cycles return True. assert nx.is_semieulerian(nx.path_graph(4)) G = nx.path_graph(6, create_using=nx.DiGraph) assert nx.is_semieulerian(G) # Test graphs with Eulerian cycles return False. assert not nx.is_semieulerian(nx.complete_graph(5)) assert not nx.is_semieulerian(nx.complete_graph(7)) assert not nx.is_semieulerian(nx.hypercube_graph(4)) assert not nx.is_semieulerian(nx.hypercube_graph(6))
import networkx as nx G = nx.Graph() G.add_edges_from([(1, 2), (1, 3), (2, 3), (2, 4), (2, 6), (3, 4), (3, 5), (4, 5), (4, 6), (5, 6), (5, 7), (6, 7)]) posicoes = { 1: (.5, 1), 2: (0, .75), 3: (1, .75), 4: (.5, .5), 5: (1, .25), 6: (0, .25), 7: (.5, 0), } nx.draw_networkx(G, pos=posicoes) print("Grafo G") print("-------") print("É Euleriano?", "Sim" if nx.is_eulerian(G) else "Não") print("É Semieuleriano?", "Sim" if nx.is_semieulerian(G) else "Não") print("É Tem caminho Euliriano?", "Sim" if nx.has_eulerian_path(G) else "Não")
# "https://networkx.github.io/documentation/networkx-1.10/reference/algorithms.shortest_paths.html". # reference: https://networkx.github.io/documentation/networkx-1.10/reference/algorithms.shortest_paths.html print(startBlue + '\nShortest path from Pensacola to Phoenix:\n' + endColor, nx.shortest_path(G, 'Pensacola', 'Phoenix')) print(startBlue + '\nDijkstra path from Pensacola to Phoenix:\n' + endColor, nx.dijkstra_path(G, 'Pensacola', 'Phoenix')) # Eulerian: # reference: https://networkx.github.io/documentation/stable/reference/algorithms/euler.html # Use-Case example: "The purpose of the proposed new roads is to make the town mailman-friendly. In graph theory terms, # we want to change the graph so it contains an Euler circuit. This is also referred to as Eulerizing a graph. The # most mailman-friendly graph is the one with an Euler circuit since it takes the mailman back to the starting point. # This means that the mailman can leave his car at one intersection, walk the route hitting all the streets just once, # and end up where he began. There is no backtracking or walking of streets twice. This saves him time." # reference: https://study.com/academy/lesson/eulerizing-graphs-in-math.html print(startBlue + '\nHas Eulerian path:\n' + endColor, nx.has_eulerian_path(G)) print(startBlue + '\nIs semi-Eulerian:\n' + endColor, nx.is_semieulerian(G)) # Shortest paths (Bellman Ford): # reference: https://networkx.github.io/documentation/stable/reference/algorithms/shortest_paths.html # defined: The Bellman-Ford algorithm is a graph search algorithm that finds the shortest path between a given source # vertex and all other vertices in the graph. This algorithm can be used on both weighted and unweighted graphs. # reference: https://brilliant.org/wiki/bellman-ford-algorithm/ print(startBlue + '\nBellman Ford path from Los Angeles:\n' + endColor, nx.bellman_ford_predecessor_and_distance(G, 'Los Angeles')) # Linear Algebra (Eigenvalues): # reference: https://networkx.github.io/documentation/stable/reference/linalg.html # defined: Using scaler multiplication (matrix multiplication = scaler multiplication) to create a new figure, # utilizing Eigenvalues and Eigenvectors # reference: https://www.youtube.com/watch?v=vs2sRvSzA3o # Real world use-case: To scale a model to a real-world dataset or graph # Reference: http://barabasi.com/f/94.pdf # Appears that utilizing the modularity spectrum, groups can be assigned into clusters/networks
# -*- coding: utf-8 -*- import networkx as nx G = nx.Graph() # Exercício 1 G.add_edge('A', 'B', weight=42) G.add_edge('B', 'C', weight=42) G.add_edge('C', 'D', weight=42) G.add_edge('A', 'C', weight=42) G.add_edge('D', 'A', weight=42) G.add_edge('C', 'D', weight=42) nx.draw(G, with_labels=True) print("É Euleriano?", nx.is_eulerian(G)) print("É semi-Euleriano?", nx.is_semieulerian(G)) print("É não-Euleriano?", nx.is_semieulerian(G))