Programming Illustration : Emergence of Connectedness

Social Networks
30 Jul 201730:05

Summary

TLDRThis script explores the surprising result that a graph with n nodes becomes connected with just n log n edges when added randomly. It demonstrates through coding in Python using NetworkX how to create a graph, add nodes and random edges, and determine the number of edges needed for connectivity. The script also includes a visual comparison of the actual number of edges required against the theoretical n log n, showing that while not exact, the number of edges needed is approximately of that order, thus validating the claim.

Takeaways

  • 📈 The script discusses a surprising result in graph theory, stating that a graph with n nodes can be made connected with just n log n edges.
  • 💡 The concept is demonstrated with a practical experiment using a screencast to write code that tests this theory with random graphs of varying sizes.
  • 🔍 The code involves creating a graph with a specified number of nodes and then adding random edges one by one until the graph becomes connected.
  • 🛠️ The script includes a step-by-step guide on how to write the code in Python using the NetworkX library to handle graph operations.
  • 📝 A function named 'add_nodes' is defined to add a specified number of nodes to a graph, initializing the process of making the graph connected.
  • 🔗 The 'add_random_edge' function is introduced to add a single random edge to the graph, helping in the gradual connection of the graph.
  • 🔄 A loop is used to continuously add random edges to the graph until the 'is_connected' function from NetworkX returns true, indicating the graph is fully connected.
  • 📊 The script suggests plotting the relationship between the number of nodes and the number of edges required for connectivity to visualize the results.
  • 📈 The plot shows a curve that, with averaging over multiple instances, becomes smoother and more indicative of the theoretical n log n relationship.
  • 🔍 The script questions whether the number of edges required is exactly n log n, and adjusts the plot to compare the actual data with the theoretical n log n curve.
  • 🧩 Finally, the script concludes that while the exact number of edges may not be n log n, it is of the order of n log n, supporting the initial claim with empirical data.

Q & A

  • What is the surprising result discussed in the script about graph connectivity?

    -The surprising result is that on a graph with n nodes, only n log n number of edges are needed to make the graph connected, allowing a path from any node to every other node in the network.

  • What is the purpose of the 'connectivity.py' file in the script?

    -The 'connectivity.py' file contains the code necessary for creating a graph, adding nodes, and adding random edges to the graph until it becomes connected.

  • What is the role of the 'add_nodes' function in the script?

    -The 'add_nodes' function initializes a graph with a specified number of nodes using the NetworkX library, which is essential for setting up the graph for further operations.

  • How does the 'add_random_edge' function work in the script?

    -The 'add_random_edge' function selects two random vertices from the graph and adds an edge between them, provided the vertices are not the same to avoid self-loops.

  • What is the purpose of the 'add_till_connectivity' function?

    -The 'add_till_connectivity' function continuously adds random edges to the graph until the graph becomes connected, demonstrating the process of achieving graph connectivity.

  • How does the script verify if the graph is connected?

    -The script uses the 'nx.is_connected' function from the NetworkX library to check if the graph is connected after adding edges.

  • What is the significance of the 'create_instance' function in the script?

    -The 'create_instance' function encapsulates the process of creating a graph with a certain number of nodes and determining the number of edges required to connect the graph.

  • What does the 'plot_connectivity' function do in the script?

    -The 'plot_connectivity' function plots the relationship between the number of nodes and the number of edges required for connectivity, providing a visual representation of the graph connectivity process.

  • Why are there spikes in the initial plot of the script?

    -The spikes in the initial plot occur due to the randomness of the experiment. Each execution can yield slightly different results, causing fluctuations in the number of edges required for connectivity.

  • How does averaging the results over multiple instances affect the plot?

    -Averaging the results over multiple instances smooths out the plot by reducing the impact of random fluctuations, providing a more consistent and reliable representation of the data.

  • What does the script conclude about the relationship between the number of nodes and edges required for connectivity?

    -The script concludes that the number of edges required to connect a graph is of the order of n log n, which aligns with the initial surprising result discussed.

Outlines

00:00

😲 Introduction to Graph Connectivity Theory

This paragraph introduces a surprising graph theory result: a graph with n nodes can be made connected with just n log n edges. The speaker plans to demonstrate this through a Python screencast, using the NetworkX library to create random graphs and observe the number of edges needed for connectivity. The coding process begins with creating a 'connectivity.py' file and importing necessary packages, followed by defining a function to add a specified number of nodes to a graph.

05:11

📝 Coding the Graph Node Addition and Random Edge Functions

The speaker proceeds with the coding tutorial by defining a function to add nodes to a graph and another to add a single random edge. The 'add_nodes' function utilizes NetworkX's built-in capabilities to add a list of nodes to a graph, while the 'add_random_edge' function selects two random nodes and adds an edge between them unless they are the same, to avoid self-loops. The paragraph details the code implementation, including handling a minor spelling error, and verifying the addition of nodes and edges in the graph.

10:14

🔁 Automated Edge Addition for Graph Connectivity

The tutorial continues with the creation of a function 'add_till_connectivity' that iteratively adds random edges to a graph until it becomes connected. The process uses a while loop with NetworkX's 'is_connected' function to check for connectivity. The speaker demonstrates the function by adding edges to a graph with ten nodes and observing the number of edges required for connectivity, which in this case, is sixteen.

15:24

🌐 Generalizing the Connectivity Process for Different Node Counts

The speaker aims to generalize the connectivity process by creating a 'create_instance' function that takes the number of nodes as input and returns the number of edges needed for connectivity. This function encapsulates the previous steps, including adding nodes and edges, and is used to test different node counts, such as ten, twenty, and thirty, to observe the pattern in the number of edges required.

20:27

📈 Plotting and Analyzing Graph Connectivity Data

To visualize the relationship between the number of nodes and edges required for connectivity, the speaker introduces a 'plot_connectivity' function. This function plots the number of edges against the number of nodes for a range of node counts. The resulting plot shows a variable number of edges required, with spikes due to the random nature of the experiment. The speaker suggests averaging the results over multiple instances to smooth out the plot.

25:27

🔍 Refining the Connectivity Experiment and Comparing with n log n

The speaker refines the experiment by averaging the connectivity results over multiple instances to obtain a smoother curve. They then compare the empirical data with the theoretical n log n relationship, adjusting the plot to show n log n divided by two to better match the observed data. The adjusted plot demonstrates that the number of edges required for connectivity is of the order of n log n, confirming the initial theoretical claim.

Mindmap

Keywords

💡Graph Theory

Graph theory is a branch of mathematics concerned with networks of points connected by lines. In the video, graph theory is central to understanding how nodes (points) and edges (lines) interact to form a connected network. The script discusses the surprising result that a graph with 'n' nodes can be made connected with just 'n log n' edges, emphasizing the efficiency of this connectivity in graph theory.

💡Connected Graph

A connected graph is a graph in which there is a path from every node to every other node. The script focuses on the concept of connectivity in graphs, demonstrating through code how a graph can be made connected by adding edges, and eventually plotting the number of edges required as the number of nodes increases.

💡Random Edges

Random edges refer to the process of adding edges between nodes in a graph without any specific pattern or order. In the script, the process of adding random edges to a graph is used to explore the minimum number of edges needed to achieve connectivity, highlighting the role of randomness in network formation.

💡NetworkX

NetworkX is a Python library used for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks. The script uses NetworkX to create graphs, add nodes and edges, and check for connectivity, demonstrating its utility in graph theoretical analysis.

💡Node

In graph theory, a node represents a point in a graph. The script discusses the process of adding nodes to a graph and how the number of nodes affects the number of edges required for the graph to be connected, using nodes as the fundamental building blocks of the network.

💡Edge

An edge in graph theory is a line that connects two nodes. The script explores the concept of adding edges to a graph, with the goal of making the graph connected. The number of edges is a key variable in the experiments described in the script.

💡Logarithmic Growth

Logarithmic growth refers to a situation where the growth rate of a quantity slows down as the quantity itself increases. The script mentions 'n log n', indicating that the number of edges needed for connectivity grows logarithmically with the number of nodes, which is a key insight into the efficiency of network connectivity.

💡Python

Python is a high-level programming language used for general-purpose programming. The script includes Python code snippets that demonstrate how to use the NetworkX library to create and manipulate graphs, as well as perform connectivity checks and plot results.

💡Code Execution

Code execution in the script refers to the process of running Python code to perform specific tasks, such as creating graphs, adding nodes and edges, and plotting the results. The script provides a step-by-step guide on executing code to explore graph connectivity.

💡Plotting

Plotting in the context of the script refers to the graphical representation of data, specifically the relationship between the number of nodes and the number of edges required for connectivity. The script discusses using matplotlib, a plotting library in Python, to visualize the results of the graph connectivity experiments.

💡Averaging

Averaging in the script is used to smooth out the results of the random experiments by performing multiple instances of the experiment and calculating the mean. This technique is applied to obtain a more reliable estimate of the number of edges required for graph connectivity, illustrating the importance of statistical analysis in experimental results.

Highlights

A graph with n nodes can be made connected with just n log n edges.

Demonstration of a screencast coding session to explore graph connectivity.

Introduction to the NetworkX library for graph operations in Python.

Creating a function to add a specified number of nodes to a graph.

Explanation of how to add random edges to a graph to achieve connectivity.

Utilization of the random.choice function to select nodes for edge creation.

Ensuring no self-loops are created when adding edges between nodes.

Development of a function to add edges until the graph becomes connected.

Use of nx.is_connected to check the connectivity status of a graph.

Plotting the relationship between the number of nodes and edges for connectivity.

Observation of the graph connectivity process through random experimentation.

Averaging results over multiple instances to smooth out the connectivity curve.

Comparing the experimental connectivity curve with the theoretical n log n curve.

Adjusting the theoretical model to n log n / 2 for a closer fit to experimental data.

Final assertion that the number of edges required is of the order of n log n, confirming the initial claim.

Practical application of graph theory concepts through coding and visualization.

Importance of random experimentation in verifying theoretical predictions in graph theory.

Transcripts

play00:04

We just now looked at a very surprising and  interesting result which says that on a graph  

play00:11

if we have n nodes then one just needs n log  n number of edges to make this graph connected  

play00:17

that is if we take n nodes and we randomly  put n log n edges in this graph the graph  

play00:24

becomes connected so you can start at any node  in this network and from this node you can find  

play00:29

a path to every other node in the network. Now we will be doing a screencast for it we  

play00:34

will be writing a piece of code in which we  will take random graphs of different different  

play00:39

sizes and then we will look at the number of  edges required to make this graph connected so  

play00:46

we will do this we will look at the plot lets  get started let's start from very basic coding  

play00:52

so what we want to do is we want to have some  nodes in the graph and then we add random edges  

play01:01

in this network one at a time and our aim is to  see after how after addition of how many edges  

play01:08

does this network becomes connected. So we first  create a file for this. So let's open a new file  

play01:16

in notepad lets name it as connectivity dot py. So here we have a file oh we first of all import  

play01:27

our basic packages which we will need import  networkx as nx. See I think its sufficient for  

play01:36

the time being ok so what we do we first define a  function to add some specified number of nodes in  

play01:43

the network so the function already exists in  networkx you all know but just for the sake of  

play01:50

simplicity and clarification I will again make a  function here and lets name this function as add  

play01:57

nodes and you pass a number n as a parameter to  this function so what this function is going to  

play02:04

do is it will create a graph g equals two  nx dot graph and add n number of nodes in  

play02:13

this graph so we can do g dot I hope you remember  the function add nodes from so this function adds  

play02:22

the nodes in the graph from a list and the list  we give here so the list is from number zero to  

play02:29

number n minus one so n nodes get added to this  graph and we finally return the graph simple  

play02:39

function we will just keep commenting everything  side by side so we comment it add n number of  

play02:47

nodes in the graph and return it perfect so we  will save it and lets come back to our window  

play02:58

so what we need to do now is import this file we  import connectivity. So this gets imported let's  

play03:12

call a function to call a function we have the  syntax connectivity dot function name our function  

play03:18

name is add underscore nodes and we had let's  say we want to add ten nodes in the graph you  

play03:27

execute it so we get a graph here. So our function  return the graph. So let's collect the output in a  

play03:33

graph name g so g equals to connectivity dot add  underscore nodes ten now I want to see whether  

play03:44

ten nodes have been actually added in the graph  or not so we can see that using the function g  

play03:51

dot number of nodes you see that there are ten  nodes in this graph next we will write a function  

play04:03

for adding one random edge to this network. So if you see currently in this network there are  

play04:10

just ten nodes there are no edges so this network  is disconnected completely disconnected. So to  

play04:17

check that we have a command nx dot is connected.  Let say you want to see whether this graph is  

play04:23

connected or not lets call ok so before using ok  nx dot is connected parse the graph g here which  

play04:34

shows us false. If the graph is not connected ok  now one by one we add edges to this network. So  

play04:41

first we write a code for adding one random edge  to this network we go back to our file which was  

play04:49

connectivity dot py and now we add a second  function here and the function is for add one  

play04:58

random edge in this network and one random edge. Lets name the function as add random edge and it  

play05:10

takes us input the graph g graph g which has been  already created with no edges so we pass that  

play05:18

graph g actually you can pass a graph g with the  edges also that's up to us so the function takes  

play05:23

as input one in graph and adds one extra random  edge to this graph define add underscore random  

play05:30

edge g now we need two random vertices from the  network so how do we pick a random vertex so to  

play05:37

put to pick a random entry we need one package  name random so we have discussed about it before  

play05:45

in the first week what does it do we are going to  use the function random dot choice first vertex  

play05:54

v one so this is the first random vertex which  we want to pick it is random dot choice random  

play06:02

dot choice function takes as the input one list  and outputs as one random value from that list.  

play06:08

So the list which we are going to pass here  is g dot nodes that is the list of nodes in  

play06:14

the network. So v one gets first random node  from this network lets choose another node  

play06:22

v two v two equals to another random node  random dot choice and again we have here  

play06:30

g dot nodes we got two random vertices now,  and we want to create an edge between them,  

play06:37

but there might be a case where v one and v two  might be same nodes so we would have picked both  

play06:48

the random nodes to be same although thats a very  rare case that might happened but we do not want  

play06:54

that to happen because we do not want any self  loops in our graph so we just put a condition  

play07:00

here if v one is not equals to v two only then we  add an edge that is this two vertices should be  

play07:08

separate so if v one is not equals to v two we  go ahead and add an edge g dot add edge from v  

play07:18

v one to v two after adding this edge we return  a graph g save it we come back to our console  

play07:30

lets now we not need to reload our file so we  reload connectivity since we have made changes  

play07:41

to it reload connectivity now the spelling  is correct reload connectivity ok so it is  

play07:48

reloaded we already have a graph here which has  ten nodes so what do we do now is g equals to  

play07:55

connectivity dot and we call our function add  random edge on the graph g itself ok seems to  

play08:12

be some problem so its a name error which says  that global name ok there is a spelling mistake.  

play08:19

So let's go back and correct it if we see here  yeah just small spelling error we correct it save  

play08:30

it come back reload it and call the function  again it works fine, so we have we now one  

play08:39

random edge should have been added in a graph. So lets see it lets look at the list of edges in  

play08:44

a graph so we do g dot edges and we can see  here that a random edge and edge has been  

play08:49

added from two vertices zero to seven so we have  written the code till now for adding a specific  

play08:56

number of vertices in the graph and adding one  random edge what is our aim our aim is to keep  

play09:03

adding these random edges in the network till our  network becomes connected and then to look at how  

play09:09

many edges were these which made this network  connectedfor doing that we go back to our code  

play09:15

and lets create another function here what is  function does is it add random edges let rather  

play09:25

say keep keeps adding random edges in g till it  becomes connected and lets name this function  

play09:45

as add till connectivity add till connectivity  and it takes the graph g as input so what it  

play09:57

is going to do till the graph doesnt become  sorry till the graphs become connected .  

play10:03

So while nx dot is connected g so this is a  function in network x which returns true if the  

play10:13

graph is connected false if it is not connected  so while n x dot is connected g is equals to false  

play10:20

it keeps running and what does it do it adds one  random edge to the graph for graph g becomes add  

play10:29

random edge g so one random edge is added to this  graph and it keeps adding more random edges till  

play10:41

the graph becomes connected and at the end lets  return the graph. So lets look at what this code  

play10:48

is doing again we reload connectivity and now what  we are going to do is g equals to connectivity dot  

play11:00

add till connectivity. So lets see how many how  many edges it adds so we already had one edge  

play11:10

which we have added previously and we pass the  graph g here and lets now look at g dot edges  

play11:18

So we see that many edges have been added  to this network to be precise the number  

play11:25

of edges added is sixteen. So its seems like in  a graph having sixteen nodes just sixteen edges  

play11:32

are required to make this graph connected. Lets  also quickly check whether its connected or not  

play11:37

so nx dot is connected g we can say that it is  we can see that it is connected it is true .  

play12:03

Till now what we have done is we have written  a small piece of code which takes a graph as  

play12:12

input keeps adding edges to this graph till  the graph becomes connected next what we want  

play12:19

to do is to repeat this procedure for different  number of nodes so here for example in this we  

play12:26

did it for ten nodes and we looked at that the  number of edges required was sixteen now want  

play12:32

to do it for twenty look at the number want to  do for thirty look at the number and so on.  

play12:37

So for doing that first of all let's create a  new function here so this function is going to  

play12:49

simplify what all we have written here lets  name the function create creates an instance  

play12:58

of the entire process so what do i mean here  by instance of an entire process it it takes  

play13:07

as input so the function will simply take as  input number of nodes and returns the number  

play13:17

of edges required for connectivity so it will  make our task easier so we will just input a  

play13:27

number and a number of nodes and it will output  us it will tell us how many edges are required  

play13:33

to connect a graph having these many nodes. So let's define this function lets name it as  

play13:40

create instance and whatever is required inside  the code for this function we have already done  

play13:48

that so define create underscore instance and lets  pass a number and here what we do next is first  

play13:56

of all we have to add nodes so g equals to add  underscore nodes and we add n nodes so we called  

play14:03

the first function which we defined previously  so whatever we have done till now in i python  

play14:09

that only we are replicating here in the file. So we first call this function add underscore  

play14:14

nodes and which returns us the graph and next we  can simply call g equals true keep adding till the  

play14:25

graph gets connected so add till connectivity we  again pass here g and it will return us what it  

play14:36

will return us the number of edges return g dot  number of edges ok lets see how does it how is  

play14:47

it working reload the file and lets call create  underscore instance and lets pass the number ten  

play14:59

here ok so it is in our file connectivity dot py  so connectivity dot create underscore instance  

play15:08

ten so we see that if there are ten nodes we  need twelve edges lets make it twenty nodes we  

play15:12

need twenty seven edges we have thirty nodes it is  fifty edges and so on and what we are interested  

play15:23

in i want to plot this and visualize how does this  plot looks like so we just needs to we just need  

play15:30

to plot what we have done so for plotting it we  go back to the file we can define a function here  

play15:46

so what is this function do plot the desire let  say we know what is desired so desired is the  

play15:56

number of edges required for connectivity  plot the desired for different number of  

play16:01

edges and lets define a function and lets call  it plot and lets call it plot connectivity ok  

play16:15

lets define this function plot connectivity and it  doesnt require as input anything we are not taking  

play16:23

any input for simply for different number of nodes  will be plot it so for plotting always we need the  

play16:30

arrays corresponding to a two axis x axis and y  axis so lets the array associated with x axis be  

play16:37

x and the array associated with y axis be y so we  have here x and y then we take a number i equals  

play16:47

to ten so i what does i tells us i is the number  of nodes for which we are experimenting so i is  

play16:56

the number of nodes while and lets lets take the  number of nodes till hundred or lets rather make  

play17:03

it four hundred so while i is less than or equals  to four hundred what is loop will do in x axis we  

play17:13

append the number of nodes which is nothing but  i and in y axis we can append or do we append  

play17:21

the number of edges required to connect these  many nodes which we can get from our function  

play17:30

create instance so y dot append create instance  and we pass i here and we need lets increment i  

play17:41

with ten every time i equals to i plus ten. Now we need to plot this for plotting.This we  

play17:47

need the package matplotlib we import matplotlib  dot pyplot as plt come back ok. So what do we do  

play18:04

plt dot plot what we want to plot is x against  y and plt dot show. So lets just do a little bit  

play18:15

of decoration here. Before plotting it lets add  some labels and the title for the curve. Let's  

play18:24

say plt dot x label, which is the label for the  x axis is nothing but the number of nodes plt  

play18:32

dot y label label for the y axis is the number of  edges required to connect the graph and plt dot  

play18:49

title. Let's give a title to this curve and lets  title it as emergence of connectivity it done.  

play18:58

Lets look at how does this plot look like  save the file we will reload it and then  

play19:17

we call connectivity dot plot connectivity I  will take some time to execute yes so here we  

play19:33

can see the curve here we have the number of  nodes here we have the number of edges and we  

play19:40

can see that as the number of nodes increases  the number of edges required to connect it also  

play19:45

increases although we see that although we see  this plot where number of nodes with number of  

play19:52

edges we see a lot of spikes in this curve. So why are these spike occurring? It's because  

play19:58

our experiment is a random experiment which  gives a different output every time you  

play20:02

execute it. Whenever you execute a randomrandom  experiment, it will give you a slightlyslightly  

play20:09

different outputs. So one time it can throw a  value three other time five other time four,  

play20:14

but if you take the expected value it gives you a  better estimate. So to remove this spikes in the  

play20:20

plot instead of taking one value corresponding  to one instance we might want to average it out  

play20:26

so lets how can we do that we go back so what  are we doing here is corresponding to one value  

play20:34

of I that is corresponding to one particular  instance given a particular number of nodes  

play20:41

stand here we execute this code only one time. So we take a graph having ten nodes we will look  

play20:47

at how many edges are required to connect it. And  then we return the number of edges what will give  

play20:53

us a better result. If we do it multiple times so  we take a graph having ten nodes then look at the  

play20:59

number of edges we again do this again do this do  it ten times and take an average over all those  

play21:05

experiments that will give us a better estimate  so lets try doing that so doing that is very easy  

play21:11

so in this create instance so create underscore  instances for one instance you give it one number  

play21:17

of nodes and it gives you number of edges. Let's average it over some particular number of  

play21:25

instances lets average it over four instances. So  n, see what I am going to do here is we define a  

play21:33

new function n and let's name it create average  instance n and what it does is, it call the  

play21:47

function create underscore instance four times  and takes an average over them. So we create a  

play21:57

list here list one and for i in range zero to four  so its repeated four times what does it do is list  

play22:10

one dot append what does it append to list one  is create underscore instance n. So it appends  

play22:19

it to list and what it returns is the average  so it return numpy dot average of list one.  

play22:31

so we have yet node imported numpy so lets  import numpy here import numpy so its returns  

play22:42

numpy dot average list one so what we are going  to do iswhile plotting in plotting connectivity  

play22:48

instead of using creating underscore instance  now we create we use create underscore average  

play22:54

underscore instance so the value is averaged  over four particular instances now lets look  

play23:02

at the change in the curve it creates we reload  connectivity and we again call this function  

play23:08

so since it is doing it four times, it takes  some time, and you can see you can say that  

play23:24

the difference in two curves is visible this curve  is more smoother because we have averaged out the  

play23:29

values over four instances where it was only one  instance in this case lets play around with it a  

play23:35

little bit further and lets change this four to  let say ten we do an average over ten instances  

play23:48

we make this four as ten we reload it and we call  the function again so let's wait for the output.  

play24:00

So again you can see a visible change in the  curve it gets all the more smooth so you can  

play24:06

change this value from ten to twenty to thirty  two hundred and the curve will become more and  

play24:10

more smooth. So we can try it for higher values  now the final and the biggest question what was  

play24:17

our claim was that if there are n nodes in the  network, it takes just n log n number of edges  

play24:24

to connect this network. The question is are  these number of edges n log n? So if I look at  

play24:30

this curve is it the curve of n log n oh lets  write a piece of code and check that as well.  

play24:38

So what I am going to do is I am going to draw  this plot. So I am going to have two plots in  

play24:45

the same figure. One plot is the plot which we  have achieved from our code which is the actual  

play24:51

data and lets plot it against n log n and look  at what is the result. So I want a very smooth  

play24:59

curve so for a very smooth curve what I do is I  average it to hundred instances. So I average it  

play25:06

to hundred instances, and now I want to plot  it against the n log n curve so what I do is  

play25:19

after doing this before plotting I do exactly  the same procedure which is written above,  

play25:26

but here I am going to plot n log n. So whatever I am going to do here is simply a  

play25:31

replication of the above code with a little bit of  change and you can write it as a separate function  

play25:36

as well and I am writing it here so I take a new  array x one and a new array y one and then again  

play25:46

I set i equals to ten which is the number  of nodes and while i is less than or equals  

play25:52

to four hundred what I do is x dot append x means  simply i append i y dot append is the curve i want  

play26:08

and the curve which i want is i multiplied by log  i ok sorry its x one dot append and its y one dot  

play26:28

append i into numpy dot log i and then i increment  i with ten and i do plt dot plot x one y one so  

play26:43

its going to show me two plots in the same figure  one is for xy which is the actual data number of  

play26:49

nodes and the number of edges required to connect  them and second is the x one y one which tells me  

play26:54

a number on the x axis and if the number is  i it tells me i log i on the y axis and again  

play27:05

reload the file and lets run it and lets give it  some time to execute since it is averaging over  

play27:11

hundred instances it will take some decent amount  of time ok the code here ends within a error and  

play27:20

next again a spelling mistake lets go back and  correct it so just correct it here and what  

play27:35

we do here isI have added an extra statement  here print i which keeps showing us how many  

play27:41

iterations has the code complete it. So for the time being let's make  

play27:46

this i two hundred since for four hundred  it takes a lot of time so let's see how  

play27:51

does it work for two hundred but if we see  a code below. So for the second plot it is  

play27:57

still plotting till the till four hundred  nodes. So we also change this and make it  

play28:03

here two hundred and now we just save  this code and let's run it so let's  

play28:10

reload our file we call a function  connectivity dot plot connectivity.  

play28:24

So here you can see two lines in this plot one is  the blue line which corresponds to our data where  

play28:32

we took different different number of nodes  and looked at the number of edges required to  

play28:36

connect the corresponding network and the red  line it corresponds to the n log n plot so the  

play28:43

linesare not totally overlapping in this case. So  here I would like to remind you that according to  

play28:49

the analysis that we have done the number of  edges required to connect the graph was not  

play28:54

exactly n log n it is of the order of n log n. So let's do a small a little bit of tweak in our  

play29:00

code lets go back to a notepad file and what  I am going to do here is instead of plotting n  

play29:07

log n. What I plot here is n log n divided by  two so please note that even if I divide this  

play29:14

term n log n by two in the final analysis  the number of edges required to connect the  

play29:20

network still remains of the order of n log n  so lets try doing that. So what I do here is  

play29:26

I divide the term log n by two and let's  see how our plot changes. Let's plot it.  

play29:45

So here you can see that both the curves become  very close to each other. Establishing the fact  

play29:51

that its essentially n log n by two number of  edges; almost of the order of n log n which are  

play29:56

required to make our graph connecting  hence satisfying a previous claim.

Rate This

5.0 / 5 (0 votes)

Related Tags
Graph TheoryPython CodingNetwork ConnectivityRandom GraphsAlgorithm AnalysisData VisualizationScientific ResearchCoding TutorialNetwork AnalysisLogarithmic Growth