Category Archives: Graph

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… Read More »

Generate a graph using Dictionary in Python

Prerequisite – Graphs To draw graph using in built libraries – Graph plotting in Python In this article, we will see how to implement graph in python using dictionary data structure in python. The keys of the dictionary used are the nodes of our graph and the corresponding values are lists with each nodes, which… Read More »

Maximum product of two non-intersecting paths in a tree

Given an undirected connected tree with N nodes (and N-1 edges), we need to find two paths in this tree such that they are non-intersecting and the product of their length is maximum. Examples: In first tree two paths which are non-intersecting and have highest product are, 1-2 and 3-4, so answer is 1*1 =… Read More »

Delete Edge to minimize subtree sum difference

Given an undirected tree whose each node is associated with a weight. We need to delete an edge in such a way that difference between sum of weight in one subtree to sum of weight in other subtree is minimized. Example: In above tree, We have 6 choices for edge deletion, edge 0-1, subtree sum… Read More »

Find length of the largest region in Boolean Matrix

Consider a matrix with rows and columns, where each cell contains either a ‘0’ or a ‘1’ and any cell containing a 1 is called a filled cell. Two cells are said to be connected if they are adjacent to each other horizontally, vertically, or diagonally .If one or more filled cells are also connected,… Read More »

Sum of dependencies in a graph

Given a directed and connected graph with n nodes. If there is an edge from u to v then u depends on v. Our task was to find out the sum of dependencies for every node. Example: For the graph in diagram, A depends on C and D i.e. 2 B depends on D i.e.… Read More »

Count number of edges in an undirected graph

Given an adjacency list representation undirected graph. Write a function to count the number of edges in the undirected graph. Expected time complexity : O(V) Examples: Input : Adjacency list representation of below graph. Output : 9 Idea is based on Handshaking Lemma. Handshaking lemma is about undirected graph. In every finite undirected graph number… Read More »