Posts

Showing posts with the label Graphs

Find the shorted path from the vertex 0 for given list of vertices.

Question For input (separated by a tab) from this link , compute the shortest path from a vertex, 0 to the following vertices. The file contains an adjacency list representation of an undirected weighted graph with 200 vertices labeled 1 to 200.  Each row consists of the node tuples that are adjacent to that particular vertex along with the length of that edge. For example, the 6th row has 6 as the first entry indicating that this row corresponds to the vertex labeled 6. The next entry of this row "141,8200" indicates that there is an edge between vertex 6 and vertex 141 that has a length of 8200.  The rest of the pairs of this row indicate the other vertices adjacent to vertex 6 and the lengths of the corresponding edges. Report the shortest path in the same order of given vertices vertices = [7,37,59,82,99,115,133,165,188,197] if you find that all ten of these vertices except 115 are at a distance of 1000 away from vertex 1 and 115 is 2000 distances away, then your answer s...

Finding the minimum steps to reach {m,n} in the grid [reviewed]

Problem A robot has to move in a grid which is in the form of a matrix. It can go to  1.) A(i,j) --> A(i+j,j) (Down)  2.) A(i,j)-->A(i,i+j) (Right)  Given it starts at (1,1) and it has to go to A(m,n), find the minimum number of STEPS it has to take to get to (m,n) and write  public static int minSteps(int m,int n)  For instance to go from (1,1) to m=3 and n=2 it has to take (1, 1) ->(1, 2) ->(3, 2) i.e. 2 steps

[K9 keyboard problem]Finding the words by ranking - updating

Image
Problem You have a list of words with ranking. Now you need to create a function that will take this list as input and provide a way so that a T9 keyboard can provide three top results of probable words based on rankings for the numbers punched in. To help the understanding here is the picture of T9 keyboard. Figure 1: example T9 keyboard For example, given the list words with ranking: [ ( "test", 13 ) , ("tax", 7), ("tom", 50), ("text", 100), ("top", 79),("tub", 101), ("tap", 3))] Following input from the T9 keyboard, should return the following output. key = 8: [ (tap, 3)(tax, 7)(test, 13)] key = 82: [ (tap, 3)(tax, 7)] key = 83: [ (test, 13)(text, 100)] key = 86: [ (tom, 50)(top, 79)] key = 2: [ ]

Finding the valid letter sequence [reviewed]

Problem  There's a new language which uses the latin alphabet. However, you don't know the order among letters.   You receive a list of words lexicographically sorted by the rules of this new language. From this list, derive one valid particular ordering of letters in the language. For example, given list of words, { "abcdefo", "begz", "hijk", "abcedefgh", "ikom"} One valid sequence would be {abcdefom}.

Flattening the Tree into Double linked list

Image
Problem Given the binary tree as shown below: Figure 1. Binary Tree Convert the tree into a double-linked list, where the left pointer and right pointer become the prev and next pointer respectively. Your algorithm should produce the double-linked list as below:

Finding the predecessor in the binary tree.

Image
Problem Given the root pointer of the binary tree and the pointer to the target node, find the predecessor of the target node. For example, in Figure 1 . the predecessor of  5 is 4.

Finding the common ancestor in the tree - Part 2 [reviewed]

Image
Problem You are given a Binary Tree (not BST) along with two pointers to nodes somewhere in the tree. Your algorithm should returns the pointer to the common parent to given nodes. For example, in the following tree Figure 1. Binary Search Tree example The common ancestor of "d" and "f" is "a".

Finding the common ancestor in the tree - Part 1[reviewed]

Image
Problem You are given a BST (Binary Search Tree) along with two pointers to nodes somewhere in the tree. Your algorithm should returns the pointer to the common parent to given nodes. For example, in the following tree Figure 1. Binary Search Tree example The common ancestor of "d" and "f" is "a".  In this question, the pointer to the parent is given as part of the feature of the BST.

Find Celebrity [reviewed]

Image
Question: You are given the 2x2 array showing who is following and who is followed. Example) If array[0][1] = true, this means Person 0 follows person 1. Celebrity is one that everyone follows him/her but he/she does not follow anyone. Your algorithm should print out who is celebrity, given the 2X2 Array with followee-follower relationship.

Calculating the sum of each level in the tree

Question: Given a binary tree with nodes containing integer values. Your algorithm should calculate the sum of values in each depth and returns sums. Example:           1          /   \        2     3       /  \   /  \     4   5 1    2 Output:  1, 5, 12