Exemple #1
0
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()
Exemple #2
0
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()
Exemple #3
0
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()
e4 = g.add_edge(0, 5)
e5 = g.add_edge(0, 6)
e6 = g.add_edge(0, 7)
e7 = g.add_edge(0, 8)
e8 = g.add_edge(0, 9)


# %%
# Compute the position of the vertices
positions = draw_matplotlib.layout(g, seed=10, name="fruchterman_reingold")
# %%
# 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,
# %%
# The result is an instance of :py:class:`.Clustering`. 
# We convert the actual clusters to lists as they are returned as
# iterators.
# 
clusters = []
for i in range(clustering.number_of_clusters()):
    clusters.append(list(clustering.ith_cluster(i)))

print('# of clusters: {}'.format(len(clusters)))
print(clusters)

# %%
# Ploting the graph with a separate color per community.

import jgrapht.drawing.draw_matplotlib as drawing
import matplotlib.pyplot as plt

positions = drawing.layout(g, name="fruchterman_reingold", seed=17)

vertex_labels = {v:str(v) for v in g.vertices}
colors = ['red', 'yellow', 'green']
for cluster, color in zip(clusters, colors):
    drawing.draw_jgrapht_vertices(g, positions=positions, vertex_list=cluster, vertex_color=color)

drawing.draw_jgrapht_vertex_labels(g, positions=positions, labels=vertex_labels)
drawing.draw_jgrapht_edges(g, positions=positions)

plt.show()
    g.add_edge(i, 10)

print(g)

# %%
# Then, we execute the 2-approximation algorithm of R. Bar-Yehuda and S. Even.

weight, vertex_cover = vc.baryehuda_even(g)

# %%
# The result is a tuple which contains the weight and the vertex cover.
# Although it failed to find the optimum which is 1.0, the returned solution is 
# at most twice the optimum.
# 
print('Vertex cover weight: {}'.format(weight))
print('Vertex cover: {}'.format(vertex_cover))


# %%
# Ploting the graph using separate color for the vertices in the vertex cover.
positions = drawing.layout(g, name="fruchterman_reingold", seed=17)

drawing.draw_jgrapht_vertices(g, positions=positions, vertex_list=vertex_cover, vertex_color='red')

non_vertex_cover = g.vertices - vertex_cover
drawing.draw_jgrapht_vertices(g, positions=positions, vertex_list=non_vertex_cover, vertex_color='gray')

drawing.draw_jgrapht_edges(g, positions=positions)

plt.show()
e1 = g.add_edge(0, 1)
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()
e8 = g.add_edge(0, 8, weight=0.2)
e9 = g.add_edge(0, 9, weight=0.7)

# %%
# create list of edges depending on their weights
weight_large = [e for e in g.edges if g.get_edge_weight(e) > 0.5]
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,
Exemple #11
0
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()