The Wayback Machine - https://web.archive.org/web/20240830103627/https://www.geeksforgeeks.org/creating-a-path-graph-using-networkx-in-python/
Open In App

Creating a Path Graph Using Networkx in Python

Last Updated : 29 Apr, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A path graph is a connected graph denoted by Pn if it contains n nodes. Nodes are connected in form of a straight line in a path graph. Here we will discuss how networkx module can be used to generate one using its inbuilt path_graph() function.

Properties of Path Graph:

  • The number of nodes in a path graph(Pn) is N.
  • The number of edges in a path graph(Pn) is N-1.
  • The diameter of the path graph(Pn) i.e maximum distance between any pair of vertices is N-1 which is between 1st and last node.
  • The chromatic number of Path Graph is 2.
  • Nodes are assigned labels from 0 to N-1
  • Terminal vertices have degree 1 and every other vertex has degree 2.
  • A path graph is a connected graph.
  • Path graph contains no cycle in it.
  • Although the path graph is connected but the removal of any edge will make it unconnected as no cycle is there in Path Graph.
  • It is a Planar Graph.

Functions used

We will use the networkx module for realizing a Path graph. It comes with an inbuilt function networkx.path_graph() and can be illustrated using the networkx.draw() method. This method is straightforward method of creating a desired path graph using appropriate parameters. 

Syntax:  path_graph(n, create_using=None)

Parameter:

  • n: Number of nodes we want in path graph.
  • create_using: We can simply pass None or pass nx.DiGraph() as a value to this argument sending nx.Digraph() will lead to creation of a directed path graph.

Approach:

  • Import module
  • Create path graph object using path_graph() function as mentioned above.
  • Pass appropriate parameters to the functions
  • Display plot

Program:

Python3




# import required module
import networkx as nx
 
# create object
G = nx.path_graph(5, create_using=nx.DiGraph())
 
# illustrate graph
nx.draw(G, node_color='green')


Output:


Previous Article
Next Article

Similar Reads

Python | Clustering, Connectivity and other Graph properties using Networkx
Triadic Closure for a Graph is the tendency for nodes who has a common neighbour to have an edge between them. In case more edges are added in the Graph, these are the edges that tend to get formed. For example in the following Graph : The edges that are most likely to be formed next are (B, F), (C, D), (F, H), and (D, H) because these pairs share
6 min read
Network Centrality Measures in a Graph using Networkx | Python
Centrality Measures allows us to pinpoint the most important nodes of a Graph. This essentially helps us to identify : Influential nodes in a Social Network. Nodes that disseminate information to many nodes Hubs in a transportation network Important pages in the Web Nodes that prevent the Network from breaking up Firstly, we need to consider the fa
6 min read
Star Graph using Networkx Python
In this article, we are going to see Star Graph using Networkx Python. A Star graph is a special type of graph in which n-1 vertices have degree 1 and a single vertex have degree n – 1. This looks like that n – 1 vertex is connected to a single central vertex. A star graph with total n – vertex is termed as Sn. Properties of Star Graph: It has n+1
2 min read
Barbell Graph Using Python networkx
Prerequisite: networkx There are many kinds of definitions of the barbell graphs. The most commonly used one is an n-barbell graph which is a simple graph obtained by connecting two copies of a complete graph with n nodes. In this article, we are going to see how to use a barbell graph using python. Examples of n-barbell graph: Example 1: If N=3 No
2 min read
Ladder Graph Using Networkx Module in Python
In this article, we are going to see the ladder graph using Python. It is a graph that looks like ladders used commonly with every node attached to two other nodes in a specific manner. We can obtain a ladder graph by joining two-path graphs of n nodes each by each node connected with a corresponding node in another path graph. Representation: Belo
2 min read
Create a Cycle Graph using Networkx in Python
A cycle graph is a graph which contains a single cycle in which all nodes are structurally equivalent therefore starting and ending nodes cannot be identified. Properties:Number of nodes in a Cycle Graph(Cn) are equal to N.Number of edges in a Cycle Graph(Cn) are equal to N.Every node is connected to 2 edges hence degree of each node is 2.It is a H
2 min read
Lollipop Graph in Python using Networkx module
The lollipop graph consists of 2 components a complete graph called clique and a path graph. More precisely L (m ,n) is a graph with an m-node complete graph and n-node path graph. L(4,2) Lollipop Graph: Here let's see the properties of this graph: So.no Properties of Lollipop Graph: 1 It is a connected graph with two components clique and path gra
3 min read
Complete Graph using Networkx in Python
A complete graph also called a Full Graph it is a graph that has n vertices where the degree of each vertex is n-1. In other words, each vertex is connected with every other vertex. Example: Complete Graph with 6 edges: Properties of Complete Graph: The degree of each vertex is n-1.The total number of edges is n(n-1)/2.All possible edges in a simpl
3 min read
Wheel Graph using Networkx Python
A wheel graph is a type of graph in which if we connect each node in an n-1 node cycle graph to nth node kept at the centre we get a wheel graph. The definition would be more clear after seeing the example below. Wheel Graph with n nodes is represented by Wn . Example: W5: W6: Properties of Wheel Graph: Total number of edges are 2(N-1)It is a plana
2 min read
Ego graph Using Networkx in Python
Prerequisite - Graphs, Networkx Basics Ego network is a special type of network consisting of one central node and all other nodes directly connected to it. The central node is known as ego, while the other surrounding nodes directly connected to it are known as alters. Ego networks are mostly used in analyzing social connections, links, and relati
4 min read
Operations on Graph and Special Graphs using Networkx module | Python
This article shows how to create an undirected Graph. This article continues for other types of Graphs and visualization techniques. The basic Graph operations are as follows: Getting Subgraph from a Graph : Given a Graph, if we are given a subset of its set of nodes, we can create a Subgraph by selecting these nodes and all the edges between them
8 min read
Difference between Graph Isomorphism and Automorphism using Python NetworkX
Graph isomorphism and automorphism are important concepts in graph theory. Graph isomorphism checks the equivalence of two graphs, while graph automorphism deals with the symmetry within a single graph. Using Python NetworkX, we can easily explore and visualize these concepts, making it a valuable tool for studying complex networks. Difference betw
3 min read
Saving a Networkx graph in GEXF format and visualize using Gephi
Prerequisites: Networkx NetworkX is a Python language software package for the creation, manipulation, and study of the structure, dynamics, and function of complex networks. It is used to study large complex networks represented in form of graphs with nodes and edges. Using networkx we can load and store complex networks. We can generate many type
2 min read
Visualizing the complement of a graph using networkx
Given a graph G complement of the graph is obtained by removing the edges contained in G from the complete graph having the same number of nodes as G. Example: Initial Graph G: The complement of G: Realizing Complement using Python : We will use networkx.complement() function for our task Syntax: networkx.complement() Returns the complement of the
2 min read
Visualizing the composition two of graph using networkx
In this article, we are going to see how to visualize the composition two of the graph using networkx. If we are given two graphs A and B then their composition can be found by performing union operation on node-set and edge-set of A and B. Simply if an edge is contained between any two vertices in any of the initial graphs it will be contained in
3 min read
Python NetworkX - Tutte Graph
It is a graph with 46 vertices and 69 edges. It is important because it is an exception to Tait's conjecture which states that every 3-regular polyhedron has a Hamiltonian cycle. Properties of Tutte Graph: It is a cubic polyhedral graph which is evident from the diagram above as it is both cubic and polyhedralIt is a Non-Hamiltonian graph.It is a P
2 min read
Hyperlink Induced Topic Search (HITS) Algorithm using Networkx Module | Python
Hyperlink Induced Topic Search (HITS) Algorithm is a Link Analysis Algorithm that rates webpages, developed by Jon Kleinberg. This algorithm is used to the web link-structures to discover and rank the webpages relevant for a particular search. HITS uses hubs and authorities to define a recursive relationship between webpages. Before understanding t
4 min read
NetworkX : Python software package for study of complex networks
NetworkX is a Python language software package for the creation, manipulation, and study of the structure, dynamics, and function of complex networks. It is used to study large complex networks represented in form of graphs with nodes and edges. Using networkx we can load and store complex networks. We can generate many types of random and classic
3 min read
Resolving the "module 'networkx' has no attribute 'from_pandas_edgelist'" Error
When working with NetworkX, a common library for creating, manipulating, and studying the structure, dynamics, and functions of complex networks, you might encounter the error: "module 'networkx' has no attribute 'from_pandas_edgelist'". This error typically arises due to version discrepancies or incorrect usage of the from_pandas_edgelist function
4 min read
Convert the undirected graph into directed graph such that there is no path of length greater than 1
Given an undirected graph with N vertices and M edges and no self loops or multiple edges. The task is to convert the given undirected graph into a directed graph such that there is no path of length greater than 1. If it is possible to make such a graph then print two space-separated integers u and v in M lines where u, v denotes source and destin
10 min read
Python VLC Instance - Creating Media path
In this article we will see how we can create a media path from the Instance class in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. Instance act as a main object of the VLC library with the Instance object we can create me
2 min read
Building an undirected graph and finding shortest path using Dictionaries in Python
Prerequisites: BFS for a GraphDictionaries in Python In this article, we will be looking at how to build an undirected graph and then find the shortest path between two nodes/vertex of that graph easily using dictionaries in Python Language. Building a Graph using Dictionaries Approach: The idea is to store the adjacency list into the dictionaries,
3 min read
PyQtGraph – Creating Graph Item
In this article we will see how we can create a graph item in PyQTGraph. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.). A Graph is a non-linear data
4 min read
Detect a negative cycle in a Graph using Shortest Path Faster Algorithm
Given a graph G consisting of nodes valued [0, N - 1], a source S, and an array Edges[][3] of type {u, v, w} that denotes that there is a directed edge between node u and v with weight w, the task is to check if there exists a negative cycle from the given source. If found to be true, then print "Yes". Otherwise, print "No". A negative cycle is a c
11 min read
Detect cycle in the graph using degrees of nodes of graph
Given a graph, the task is to detect a cycle in the graph using degrees of the nodes in the graph and print all the nodes that are involved in any of the cycles. If there is no cycle in the graph then print -1. Examples: Input: Output: 0 1 2 Approach: Recursively remove all vertices of degree 1. This can be done efficiently by storing a map of vert
11 min read
Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
In Set 1, unweighted graph is discussed. In this post, weighted graph representation using STL is discussed. The implementation is for adjacency list representation of weighted graph. We use two STL containers to represent graph: vector : A sequence container. Here we use it to store adjacency lists of all vertices. We use vertex number as index in
7 min read
Construct a Tree whose sum of nodes of all the root to leaf path is not divisible by the count of nodes in that path
Given an N-ary tree consisting of N nodes numbered from 1 to N rooted at node 1, the task is to assign values to each node of the tree such that the sum of values from any root to the leaf path which contains at least two nodes is not divisible by the number of nodes along that path. Examples: Input: N = 11, edges[][] = {{1, 2}, {1, 3}, {1, 4}, {1,
11 min read
Find the path from root to a vertex such that all K vertices belongs to this path or 1 distance away
Given a tree consisting of N vertices numbered from 1 to N rooted at vertex 1. You are given Q queries. Each query consists of the set of K distinct vertices vi[1], vi[2],…, vi[K]. The task is to determine if there is a path from the root to some vertex X such that each of the given K vertices either belongs to this path or has the distance 1 to so
10 min read
Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
Given a connected graph with N vertices and M edges. The task is to find the shortest path from source to the destination vertex such that the difference between adjacent edge weights in the shortest path change from positive to negative and vice versa ( Weight(E1) > Weight(E2) < Weight(E3) .... ). If no such path exists then print -1. Exampl
13 min read
Minimum cost such that path sum is at max K (Target Path Sum)
Given a rooted tree consisting of N vertices. The vertices are numbered from 0 to N-1, and the root is the vertex 0. You are also given a value array Arr: Arr0, Arr1,....., Arrn-1. You can do any of these moves any number of times: Pick any node that is not root cut the edge between it and its parent and connect an edge between this node to the roo
11 min read
Article Tags :
Practice Tags :