def draw_components(g,clist,clabel="Componente",vlabel='',v_attrs={},elabel='',e_attrs={},vertexid_aslabel=False, layout="circular",width=8,height=5,vsize=450,vshape='o',vfcolor='black'): if len(clist) > 10: return None positions = draw_matplotlib.layout(g, seed=10, name=layout) color_names = ["red", "cyan", "green", "lightgreen", "grey", "lightpink", "orange", "yellow", "violet", "lightblue"] i = 1 for c in clist: label = clabel + str(i) draw_matplotlib.draw_jgrapht_vertices( g, positions=positions, vertex_list=c, vertex_color=color_names[i-1], vertex_title=label, vertex_size=vsize, vertex_shape=vshape ) i = i+1 draw_matplotlib.draw_jgrapht_edges( g, positions=positions, edge_list=g.edges, edge_color="orange" ) vertex_labels = {} if(vlabel!='' and v_attrs!={}): for v in g.vertices: if vlabel in v_attrs[v].keys(): vertex_labels[v] = v_attrs[v][vlabel] elif vertexid_aslabel: for v in g.vertices: vertex_labels[v] = str(v) edge_labels = {} if(elabel!='' and e_attrs!={}): for e in g.edges: if elabel in e_attrs[e].keys(): edge_labels[e] = e_attrs[e][elabel] draw_matplotlib.draw_jgrapht_vertex_labels( g, positions=positions, labels=vertex_labels, vertex_font_color=vfcolor ) if not (e_attrs=={}): draw_matplotlib.draw_jgrapht_edge_labels( g, positions=positions, labels=edge_labels ) plt.rcParams['figure.figsize'] = [width,height] plt.show()
def draw_bipartite(g,p1,p2,vlabel='',v_attrs={},elabel='',e_attrs={},vertexid_aslabel=False): positions = draw_matplotlib.layout(g, seed=10, name="circular") draw_matplotlib.draw_jgrapht_vertices( g, positions=positions, vertex_list=p1, vertex_color="red", vertex_title="Partição 1" ) draw_matplotlib.draw_jgrapht_vertices( g, positions=positions, vertex_list=p2, vertex_color="blue", vertex_title="Partição 2" ) draw_matplotlib.draw_jgrapht_edges( g, positions=positions, edge_list=g.edges, edge_color="orange" ) vertex_labels = {} if(vlabel!='' and v_attrs!={}): for v in g.vertices: if vlabel in v_attrs[v].keys(): vertex_labels[v] = v_attrs[v][vlabel] elif vertexid_aslabel: for v in g.vertices: vertex_labels[v] = str(v) edge_labels = {} if(elabel!='' and e_attrs!={}): for e in g.edges: if elabel in e_attrs[e].keys(): edge_labels[e] = e_attrs[e][elabel] draw_matplotlib.draw_jgrapht_vertex_labels( g, positions=positions, labels=vertex_labels, vertex_font_color="white" ) if not (e_attrs=={}): draw_matplotlib.draw_jgrapht_edge_labels( g, positions=positions, labels=edge_labels ) plt.show()
def draw_graph(g, layout="circular", vertexid_aslabel=False, edgeweight_aslabel=False, vlabel='', v_attrs={}, elabel='', e_attrs={}, vertex_color='cyan', vmap=None, edge_color='orange', emap=None, vertex_font_color='black', edge_font_color='gray', axis=False, width=8, height=5, vsize=450, vshape='o', vset=[], vsetcolor=[], vsetlabel=[], eset=[], esetcolor=[], esetlabel=[]): flatten = lambda t: [item for sublist in t for item in sublist] positions = draw_matplotlib.layout(g, seed=10, name=layout) if vset == []: draw_matplotlib.draw_jgrapht_vertices(g, positions=positions, vertex_list=g.vertices, vertex_color=vertex_color, vertex_size=vsize, vertex_cmap=vmap, vertex_shape=vshape) else: notinvset = [v for v in g.vertices if v not in flatten(vset)] for i in range(len(vset)): draw_matplotlib.draw_jgrapht_vertices(g, positions=positions, vertex_list=vset[i], vertex_color=vsetcolor[i], vertex_title=vsetlabel[i], vertex_size=vsize, vertex_shape=vshape) if notinvset != []: draw_matplotlib.draw_jgrapht_vertices(g, positions=positions, vertex_list=notinvset, vertex_color=vertex_color, vertex_size=vsize, vertex_cmap=vmap, vertex_shape=vshape) if eset == []: draw_matplotlib.draw_jgrapht_edges(g, positions=positions, edge_list=g.edges, edge_color=edge_color, arrow_color=edge_color) else: notineset = [e for e in g.edges if e not in flatten(eset)] for i in range(len(eset)): draw_matplotlib.draw_jgrapht_edges(g, positions=positions, edge_list=eset[i], edge_color=esetcolor[i], arrow_color=esetcolor[i], edge_title=esetlabel[i]) if notineset != []: draw_matplotlib.draw_jgrapht_edges(g, positions=positions, edge_list=notineset, edge_color=edge_color, arrow_color=edge_color) vertex_labels = {} if (vlabel != '' and v_attrs != {}): for v in g.vertices: if vlabel in v_attrs[v].keys(): vertex_labels[v] = v_attrs[v][vlabel] elif vertexid_aslabel: for v in g.vertices: vertex_labels[v] = str(v) edge_labels = {} if (elabel != '' and e_attrs != {}): for e in g.edges: if elabel in e_attrs[e].keys(): edge_labels[e] = e_attrs[e][elabel] elif edgeweight_aslabel: for e in g.edges: edge_labels[e] = str(g.get_edge_weight(e)) draw_matplotlib.draw_jgrapht_vertex_labels( g, positions=positions, labels=vertex_labels, vertex_font_color=vertex_font_color) if (not e_attrs == {}) or (edgeweight_aslabel): draw_matplotlib.draw_jgrapht_edge_labels( g, positions=positions, labels=edge_labels, edge_font_color=edge_font_color) pyplot.rcParams['figure.figsize'] = [width, height] pyplot.show()
# %% # Then, we execute the greedy coloring algorithm. num_colors, color_map = greedy_dsatur(g) print(num_colors) print(color_map) # %% # We next plot the graph with the colors. int_to_actual_color = {0: 'orangered', 1: 'lightsteelblue', 2: 'lightgreen'} vertex_color = [int_to_actual_color[color_map[v]] for v in g.vertices] vertex_labels = {v: str(v) for v in g.vertices} import jgrapht.drawing.draw_matplotlib as drawing import matplotlib.pyplot as plt positions = drawing.layout(g, name="fruchterman_reingold", seed=17) drawing.draw_jgrapht_vertices(g, positions=positions, vertex_color=vertex_color) drawing.draw_jgrapht_vertex_labels(g, positions=positions, labels=vertex_labels) drawing.draw_jgrapht_edges(g, positions=positions) plt.show()
# %% # Draw the graph # Draw vertices with title draw_matplotlib.draw_jgrapht_vertices( g, positions=positions, node_list=(0, 1, 2, 3, 4), node_title="green nodes" ) draw_matplotlib.draw_jgrapht_vertices( g, positions=positions, node_list=(5, 6, 7, 8, 9), node_color="red", node_title="red nodes" ) # Draw the edges with edge list, edge color and edge title draw_matplotlib.draw_jgrapht_edges( g, positions=positions, edge_list=(e0, e1, e2, e3, e4), edge_color="orange", edge_title="orange edges", ) draw_matplotlib.draw_jgrapht_edges( g, positions=positions, edge_list=(e5, e6, e7, e8), edge_color="blue", edge_title="blue edges", ) # Draw the vertex labels with custom vertex names draw_matplotlib.draw_jgrapht_vertex_labels( g, positions=positions,
tour = metric_tsp_christofides(g) # %% # The result is an instance of :py:class:`.GraphPath` # tour_start = tour.start_vertex tour_edges = tour.edges tour_weight = tour.weight print('Tour starts at: {}'.format(tour_start)) print('Tour edges: {}'.format(tour_edges)) print('Tour weight: {}'.format(tour_weight)) # %% # We next plot the graph and the tour. import jgrapht.drawing.draw_matplotlib as drawing import matplotlib.pyplot as plt positions = drawing.layout(g, name="fruchterman_reingold", seed=17) vertex_color = ['red' if v == tour_start else 'green' for v in g.vertices] non_tour_edges = g.edges - tour_edges drawing.draw_jgrapht_vertices(g, positions=positions, vertex_color=vertex_color) drawing.draw_jgrapht_edges(g, positions=positions, edge_list=tour_edges, edge_linewidth=3, edge_color='red') drawing.draw_jgrapht_edges(g, positions=positions, edge_list=non_tour_edges, edge_color='gray') plt.show()
e2 = g.add_edge(0, 2) e3 = g.add_edge(1, 3) e4 = g.add_edge(2, 3) e5 = g.add_edge(2, 4) e4 = g.add_edge(3, 4) # %% # Compute the position of the vertices positions = [(0, 0), (1, 0), (0, 1), (1, 1), (0.5, 2.0)] # %% # Draw the graph # Draw the vertexs using the vertex list and vertex color drawing.draw_jgrapht_vertices(g, positions=positions, vertex_list=(0, 1, 2, 3), vertex_color="green") # Draw the vertexs using the vertex list, vertex color and vertex shape drawing.draw_jgrapht_vertices(g, positions=positions, vertex_list=[4], vertex_color="red", vertex_shape="^") # Draw the edges using edge color drawing.draw_jgrapht_edges(g, positions=positions, edge_color="orange") plt.tight_layout() plt.show()
# Then, we execute Prim's algorithm. mst_weight, mst_edges = spanning.prim(g) # %% # The result is a tuple which contains the weight and the minimum spanning tree. # print('mst weight: {}'.format(mst_weight)) print('mst tree: {}'.format(mst_edges)) # %% # Ploting the graph with highlighted the MST edges # positions = drawing.layout(g, name="fruchterman_reingold", seed=17) drawing.draw_jgrapht_vertices(g, positions=positions) non_mst_edges = g.edges - mst_edges drawing.draw_jgrapht_edges(g, positions=positions, edge_list=mst_edges, edge_color='blue', edge_linewidth=3) drawing.draw_jgrapht_edges(g, positions=positions, edge_list=non_mst_edges, edge_linewidth=1) plt.show()
weight_small = [e for e in g.edges if g.get_edge_weight(e) <= 0.5] # %% # Compute the position of the vertices positions = draw_matplotlib.layout(g, seed=10, name="fruchterman_reingold") # %% # Draw the graph # Draw nodes draw_matplotlib.draw_jgrapht_vertices(g, positions=positions) # Draw the edges using edge list,edge color,edge title and line style draw_matplotlib.draw_jgrapht_edges( g, positions=positions, edge_list=weight_large, edge_color="red", edge_title="weight>0.5", line_style="dashed", ) draw_matplotlib.draw_jgrapht_edges( g, positions=positions, edge_list=weight_small, edge_color="orange", edge_title="weight<=0.5", ) # Draw node labels draw_matplotlib.draw_jgrapht_vertex_labels(g, positions=positions) # Draw edge labels with their weight
def draw_cut(g,cut=[],cutlabel='',vlabel='',vset=[],vsetlabel='', v_attrs={},elabel='',e_attrs={},vertexid_aslabel=False, edgeweight_aslabel=False,layout="circular",width=8,height=5,vsize=450, cmap=None,vsetcolor="red"): if cutlabel == '': cutlabel = 'Edge cut' positions = draw_matplotlib.layout(g, seed=10, name=layout) notcut = [e for e in g.edges if e not in cut] notvset = [v for v in g.vertices if v not in vset] if not (notvset == []): draw_matplotlib.draw_jgrapht_vertices( g, positions=positions, vertex_list=notvset, vertex_color="cyan", vertex_size=vsize, ) if vset != []: draw_matplotlib.draw_jgrapht_vertices( g, positions=positions, vertex_list=vset, vertex_color=vsetcolor, vertex_title=vsetlabel, vertex_size=vsize, vertex_cmap=cmap ) if cut != []: draw_matplotlib.draw_jgrapht_edges( g, positions=positions, edge_list=cut, edge_color="orange", arrow_color="orange", edge_title = cutlabel ) draw_matplotlib.draw_jgrapht_edges( g, positions=positions, edge_list=notcut, edge_color="black", arrow_color="black", ) vertex_labels = {} if(vlabel!='' and v_attrs!={}): for v in g.vertices: if vlabel in v_attrs[v].keys(): vertex_labels[v] = v_attrs[v][vlabel] elif vertexid_aslabel: for v in g.vertices: vertex_labels[v] = str(v) edge_labels = {} if(elabel!='' and e_attrs!={}): for e in g.edges: if elabel in e_attrs[e].keys(): edge_labels[e] = e_attrs[e][elabel] elif edgeweight_aslabel: for e in g.edges: edge_labels[e] = str(g.get_edge_weight(e)) draw_matplotlib.draw_jgrapht_vertex_labels( g, positions=positions, labels=vertex_labels, vertex_font_color="black" ) if (not e_attrs=={}) or (edgeweight_aslabel): draw_matplotlib.draw_jgrapht_edge_labels( g, positions=positions, labels=edge_labels ) plt.rcParams['figure.figsize'] = [width,height] plt.show()