Weighted and Directed Graphs


Introduction to weighted and Directed Graphs

Weighted and Directed Graph theory is a vital concept in computer science and mathematics, offering a powerful way to represent and analyze relationships between various entities.

Among the different types of graphs, weighted and directed graphs hold immense importance due to their ability to model intricate relationships with additional characteristics.

Weighted and Directed Graphs

Graphs consist of nodes (vertices) and edges that establish connections between these nodes.

  • When discussing weighted graphs, the edges carry a value or weight that signifies the cost, distance, or any other parameter associated with the connection between nodes. In contrast, directed graphs feature edges with a specific direction, implying a one-way relationship between nodes.
Using heapq Module (Binary Heap): Python provides a heapq module that allows you to implement a binary heap-based priority queue.
import heapq
class PriorityQueueBinaryHeap:
def __init__(self):
self.elements = []
def enqueue(self, priority, value):
heapq.heappush(self.elements, (priority, value))
def dequeue(self):
if not self.is_empty():
return heapq.heappop(self.elements)
def is_empty(self):
return len(self.elements) == 0
  • Fig 1.0:  Time complexity of a simple loop when the loop variable is incremented or decremented by a constant amount.
  • Fig 2.0: Time complexity of a loop when the loop variable is divided or multiplied by a constant amount. 
  • Fig 3.0: Time complexity of a nested loop.

Understanding Graphs in Python: 

Creating a Weighted Graph

To create a weighted graph in Python using NetworkX, initialize a graph object and then add nodes and edges with associated weights.

Adding Nodes and Edges

Incorporating nodes and edges into the weighted graph involves specifying the nodes and their connections along with their respective weights.

Working with Directed Graphs in Python

Directed Graph Representation:

In Python, implementing directed graphs involves defining edges with a specified direction, enabling the modeling of one-way relationships between nodes.

Operations on Directed Graphs:

Manipulating directed graphs encompasses various operations like adding or removing nodes, modifying edges, and performing traversal or path-finding algorithms.


To wrap it ip: 

In conclusion, understanding weighted and directed graphs in Python provides a powerful toolset for modeling and solving real-world problems involving complex relationships. The ability to represent relationships with direction and magnitude enhances the applicability of graph structures in various domains.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription