Programming Articles - Page 1029 of 3363

Program to find out the sum of minimum cost within a graph among all vertices in Python

Arnab Chakraborty
Updated on 06-Oct-2021 12:55:40

494 Views

Suppose there is a weighted graph with n vertices and m edges. The edges have the weights in powers of 2. Any vertex can be reached from any vertex in the graph, and the cost of travel will be the addition of all the edge weights in the graph. We shall have to determine the sum of minimum cost between each pair of vertices.So, if the input is likeand number of vertices (n) = 6; then the output will be 2696.The sum of all the distances is 2696.To solve this, we will follow these steps −Define a function par_finder() . ... Read More

Program to count maximum score from removing substrings in Python

Arnab Chakraborty
Updated on 06-Oct-2021 12:58:19

234 Views

Suppose we have a string s and two values x and y. We can perform given two types of operations any number of times.Search substring "ab", if present, then we can gain x points by removing it.Search substring "ba", if present, then we can gain y points by removing it.We have to find maximum points we can gain after applying the above operations on s.So, if the input is like s = "cbbaacdeabb" x = 4 y = 5, then the output will be 14 because initial string is "cbbaacdeabb", then remove "cbbaacde(ab)b" to get 4, now string is "cbbaacdeb", ... Read More

Program to find out the minimum path to deliver all letters in Python

Arnab Chakraborty
Updated on 06-Oct-2021 12:51:28

198 Views

Suppose there are n cities and that are connected with n -1 roads. A city can be visited from any other city. Now the postal system of the cities delivers k letters daily. The letter's destination can be any of the k different cities. A postal worker has to deliver all the letters to their addresses each day. We shall have to find out the minimum distance the worker has to travel to deliver all the letters. The worker can start from any given city.So, if the input is likeand the letters have to be delivered in cities (delv) 1, ... Read More

Program to find out special types of subgraphs in a given graph in Python

Arnab Chakraborty
Updated on 06-Oct-2021 12:48:43

509 Views

Suppose we have a special type of graph that has two types of vertices that are named head and feet. The graph has only one head and there are k edges that connect the head to each of the feet. So, if we are given an undirected, unweighted graph; we shall have to find out these special types of graphs in the vertex disjoint subgraphs of the graph. Any two graphs are said to be vertex disjoint if they have no vertex in common.So, if the input is likenumber of nodes (n) = 5, number of feet (t) = 2, ... Read More

Program to find out the minimum cost path between the lowest value vertex to the highest value vertex (Python)

Arnab Chakraborty
Updated on 06-Oct-2021 12:44:27

535 Views

Suppose we are given an undirected, weighted graph and are asked to find out the path with the minimum possible travel cost from a particular node to another particular node. The travel cost is calculated as the following: suppose there is a path between vertex A to vertex C as A-> B-> C. The cost of travel from A to B is 10 and the cost of travel from B to C is 20. The cost of travel from A to C will be (cost of traveling from A to B) + (difference of traveling cost from B to C ... Read More

Program to find out the path between two vertices in a graph that has the minimum penalty (Python)

Arnab Chakraborty
Updated on 06-Oct-2021 12:36:32

377 Views

Suppose we are given an undirected, weighted graph and are asked to find out the path with the minimum possible penalty from node a to node b. The penalty of a path is the bitwise OR of the weights of all the edges in the path. So, we must find out such a 'minimum penalty' path, and if there exists no path between the two nodes, we return -1.So, if the input is likestart (s) = 1, end (e) = 3; then the output will be 15.There exist two paths between vertices 1 and 3. The optimal path is 1->2->3, ... Read More

Program to find out the minimum size of the largest clique in a graph (Python)

Arnab Chakraborty
Updated on 06-Oct-2021 12:22:51

251 Views

Suppose we are given a graph and are asked to find out the minimum size of the largest clique in the graph. A clique of a graph is a subset of a graph where every pair of vertices are adjacent, i.e. there exists an edge between every pair of vertices. Finding the largest clique in a graph is not possible in polynomial time, so given the number of nodes and edges of a small graph we shall have to find out the largest clique in it.So, if the input is like nodes = 4, edges =4; then the output will ... Read More

Program to find number of ways to split array into three subarrays in Python

Arnab Chakraborty
Updated on 06-Oct-2021 12:39:56

751 Views

Suppose we have an array called nums, we have to find the number of good ways to split this array nums. Answer may be too large so return result modulo 10^9 + 7. Here a split of an array (with integer elements) is good if the array is split into three non-empty contiguous subarrays respectively from left to right, and the sum of the elements in left side is less than or equal to the sum of the elements in mid part, and the sum of the elements in mid part is less than or equal to the sum of ... Read More

Program to find out an MST using Prim's algorithm in Python

Arnab Chakraborty
Updated on 06-Oct-2021 12:19:34

2K+ Views

Suppose we are given a graph and asked to find out the 'Minimum Spanning Tree' (MST) from that graph. An MST of a graph is a subset of a weighted graph where all the vertices are present and connected, and there exists no cycle in the subset. MST is called minimum because the total edge weight of the MST is the minimum possible from the graph. So, here we use Prim's MST algorithm and find out the total edge weight of the MST from a given graph.So, if the input is like, number of vertices (n) is 4, and start ... Read More

Program to find out the minimum moves in a snakes and ladders game in Python

Arnab Chakraborty
Updated on 06-Oct-2021 12:10:39

1K+ Views

Suppose we are playing a game of snakes and ladders. We have a condition that we can roll any number that we can like on a dice. We start from position 0 and our destination is position 100, and we roll the dice several times to reach the destination. We must find out the least number of dice rolls required to reach the destination if we are provided with the position of the snakes and ladders on the board.The arrays snakes and ladders represent the positions of snakes and ladders in the board and each entry in the arrays contains ... Read More

Advertisements