Posts

Showing posts with the label Graph

Assign Edge Weights To A Networkx Graph Using Pandas Dataframe

Image
Answer : Let's try: import pandas as pd import numpy as np import networkx as nx import matplotlib.pyplot as plt df = pd.DataFrame({'number':['123','234','345'],'contactnumber':['234','345','123'],'callduration':[1,2,4]}) df G = nx.from_pandas_edgelist(df,'number','contactnumber', edge_attr='callduration') durations = [i['callduration'] for i in dict(G.edges).values()] labels = [i for i in dict(G.nodes).keys()] labels = {i:i for i in dict(G.nodes).keys()} fig, ax = plt.subplots(figsize=(12,5)) pos = nx.spring_layout(G) nx.draw_networkx_nodes(G, pos, ax = ax, labels=True) nx.draw_networkx_edges(G, pos, width=durations, ax=ax) _ = nx.draw_networkx_labels(G, pos, labels, ax=ax) Output: Do not agree with what has been said. In the calcul of different metrics that takes into account the weight of each edges like the pagerank or the betweeness centrality your weight would...

Breadth First Search Time Complexity Analysis

Image
Answer : I hope this is helpful to anybody having trouble understanding computational time complexity for Breadth First Search a.k.a BFS. Queue graphTraversal.add(firstVertex); // This while loop will run V times, where V is total number of vertices in graph. while(graphTraversal.isEmpty == false) currentVertex = graphTraversal.getVertex(); // This while loop will run Eaj times, where Eaj is number of adjacent edges to current vertex. while(currentVertex.hasAdjacentVertices) graphTraversal.add(adjacentVertex); graphTraversal.remove(currentVertex); Time complexity is as follows: V * (O(1) + O(Eaj) + O(1)) V + V * Eaj + V 2V + E(total number of edges in graph) V + E I have tried to simplify the code and complexity computation but still if you have any questions let me know. Considering the following Graph we see how the time complexity is O(|V|+|E|) but not O(V*E). Adjacency List V E v0:{v1,v2} v1:{v3} v2:{v3} v3:{} Ope...

Chart.js Doughnut With Rounded Edges And Text Centered

Image
Answer : With v2.1.3, you can use the pluginService to do this Preview Script // round corners Chart.pluginService.register({ afterUpdate: function (chart) { if (chart.config.options.elements.arc.roundedCornersFor !== undefined) { var arc = chart.getDatasetMeta(0).data[chart.config.options.elements.arc.roundedCornersFor]; arc.round = { x: (chart.chartArea.left + chart.chartArea.right) / 2, y: (chart.chartArea.top + chart.chartArea.bottom) / 2, radius: (chart.outerRadius + chart.innerRadius) / 2, thickness: (chart.outerRadius - chart.innerRadius) / 2 - 1, backgroundColor: arc._model.backgroundColor } } }, afterDraw: function (chart) { if (chart.config.options.elements.arc.roundedCornersFor !== undefined) { var ctx = chart.chart.ctx; var arc = chart.getDatasetMeta(0).data[chart.config.options.eleme...

Chartjs Treemap Example

Answer : Chart.js is great, but has small number of chart types. Chart.js is lib to use provided charts, not to create own chart types. D3.js is lib for producing data visualizations. See examples. You can find few treemap examples (i.e. this or this) done with d3.js This snippet is an example of the treemap module for chart.js. Also here are more examples and the documentation. var topTags = [ {tag:'python',num:133269},{tag:'javascript',num:109742},{tag:'java',num:65490},{tag:'c#',num:45445},{tag:'html',num:43767},{tag:'android',num:42657},{tag:'reactjs',num:38844},{tag:'php',num:34381},{tag:'sql',num:29996},{tag:'python',num:29942},{tag:'css',num:29654},{tag:'r',num:28319},{tag:'c++',num:27389},{tag:'node.js',num:25415},{tag:'angular',num:24093},{tag:'pandas',num:23826},{tag:'arrays',num:23075},{tag:'jquery',num:18968},{tag:'...