Dr-Groot / SageMath

Small Understanding how SageMath works for Graph Theory

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Welcome to SageMath for Graph Theory.


Follow us on Instagram || Follow us on Twitter || Follow us on GitHub || DR.GROOT

image


Installation

For MacOS and Window go with Download SageMath to Download the Software, its open source so no trouble in downloading.

After SageMath is installed, see that you have Jupyter Notebook if not go with link to download CLICK HERE.


We will be using Jupyter Notebook as our primary workspace, So lets get Start now.



SageMath for Graph Theory

For making a complete graph we use graphs.CompleteGraph() as in arguement we can give the number of vertices or nodes.

A Complete Graph is a graph in which all nodes are connected to all other nodes.


For Example:

Lets say we want to make a Complete Graph with 5 vertices then we will go with graphs.CompleteGraph(5) on our Jupyter Notebook.

Screenshot 2021-09-24 at 7 53 47 PM


Lets define a variable K5 and store graphs.CompleteGraph(5) in it. And to display the K5 we use K5.show()

Screenshot 2021-09-24 at 7 56 50 PM


Lets make it more excited by building a 3D COMPLETE GRAPH with 5 Vertice just going with K5.show3d().

Screenshot 2021-09-24 at 8 09 18 PM

Amazing thing about 3d Graph is, you can Rotate it using your cursor or you can go with spin = True as an arguement of show3d()

3D Graphs feature is supported by Safari and Internet Explorer Web Browser and JDK should be installed.


PETERSEN GRAPH

The Petersen Graph is a named graph that consists of 10 vertices and 15 edges, usually drawn as a five-point star embedded in a pentagon.

The Petersen Graph is a common counterexample. For example, it is not Hamiltonian.


For this we have to go wtih graphs.PetersenGraph() and then show()

Screenshot 2021-09-24 at 8 25 15 PM


And For 3d we will go with same command show3d()

Screenshot 2021-09-24 at 8 27 09 PM

Basically 3D graphs are used for finding Isomorphism or comparing two graphs together.


CUBE GRAPH

Just go with graphs.CubeGraph(n)

Return the n-cube graph, also called the hypercube in n dimensions.

The hypercube in n dimension is build upon the binary strings on n bits, two of them being adjacent if they differ in exactly one bit. Hence, the distance between two vertices in the hypercube is the Hamming distance.

Screenshot 2021-09-24 at 8 41 02 PM


And for 3d Graph version we will go with show.3d() and we will get :

Screenshot 2021-09-24 at 8 48 30 PM

Cube Graph are use in CPU's now they gave small little CPU's as you can in those node and they are connected to each other and this mean of quad core dual core and so on.


EMPTY GRAPH

Now lets create an empty graph wich means graph of 0 vertices, it is created by Graph() or by graphs.EmptyGraph().

I can proove that both can create an Empty Graph by comparing them:

Screenshot 2021-09-24 at 9 08 37 PM


LETS PLAY WITH THE GRAPHS

Now if i want to create a graph with 5 vertex, we just go with Graph(5) :

Screenshot 2021-09-24 at 9 12 41 PM


We can add vertex to the Graph G by g.add_vertex(6) and then display g

Screenshot 2021-09-24 at 9 17 00 PM


Something is like missing out, Edges Right!

So if we want to add edge between vertex 0 and 1 then we will go with g.add_edge(0, 1)

Screenshot 2021-09-24 at 9 19 33 PM

And So on we can go with this command to add edges.


Now lets display the vertex and edges by :

Screenshot 2021-09-24 at 9 26 50 PM

It is showing 0 and 1 edges that i have created now.


We can add multiple Edges by :

Screenshot 2021-09-24 at 9 32 26 PM


Now lets look over to the Total Edges we have:

Screenshot 2021-09-24 at 9 34 08 PM


if i want a Graph with 0 vertex connected to 1,2,3,4,5,6 and 7 Then:

Screenshot 2021-09-24 at 9 49 02 PM


Want a Graph with vertex 0 connected to 1, 2 and 1 to 2,3 and 2 to 3 So then:

Screenshot 2021-09-24 at 9 51 48 PM


Giving each edge a name, we have to write like:

G = Graph({0:{1:'x',2:'z', 3:'a'}, 2:{5:'out'}}) and with that we have to specfy G.show(edge_label=True) to ensure labels are visible.

Screenshot 2021-09-24 at 10 06 45 PM


If we want to change the edge name between 0 to 2 as changed we can do like this:

G.add_edge(0,2, label='changed') and G.show(edge_label=True)

Screenshot 2021-09-24 at 10 13 08 PM


If we want to find out the degree of a vertex, then we use .degree(n) where n is a vertex.

Screenshot 2021-09-24 at 10 47 19 PM


To find the neighborhood of the vertex, we use .neighbors(n), where n is the vertex.

Screenshot 2021-09-24 at 10 51 09 PM


Getting the Adjacent Matrix of the Graph, by using .adjacency_matrix().

Screenshot 2021-09-24 at 10 54 35 PM


Getting the Incidence Matrix of the Graph, by using .incidence_matrix().

Screenshot 2021-09-24 at 10 56 14 PM


Now other type of Graph is DiGraph, for that use Digraph()

Screenshot 2021-09-27 at 9 20 44 PM

you can also go for 3d version of it by .show3d()


For builting a multigraph with multi edges:

Screenshot 2021-09-27 at 9 30 06 PM


To draw Ladder Graph we use function graphs.LadderGraph()

Screenshot 2021-09-27 at 9 40 11 PM


Want to know which command does what?

Its easy to find this just give ? after each command to know about what it does.

Lets say whats graphs.CompleteGraph() do we just write graphs.CompleteGraph? and we get a pop up window like this -

Screenshot 2021-09-24 at 8 44 27 PM

Following with the instructions how to use this following statement.

About

Small Understanding how SageMath works for Graph Theory