algorithm

Graph “Vertex cover” brute algorithm

风流意气都作罢 提交于 2021-02-11 17:44:49
问题 Given an electrical network, which is a set of electric generators, between which wires are stretched. A wire has current if at least one generator is operating at one end of the wire. Find the set with minimum count of generators that need to be turned on to provide current to the entire network. I found some extra information that can help. It is "Vertex cover problem". Now we know that it hasn't special algorithm. Let's bruteforce? 回答1: As you note in the question, this is an instance of

How many level order BST sequences are possible given a preOrder and inOrder sequence?

放肆的年华 提交于 2021-02-11 17:44:13
问题 When I am trying to print level Order of BST, this question prompted me. Here is a Pre-Order Sequence: 4, 1, 2, 3, 5, 6, 7, 8 In_order Sequence : 1, 2, 3, 4, 5, 6, 7, 8 A level order sequence for a BST with above pre_order and In_order is [4, 2, 6, 1, 3, 5, 7, 8] However, for the same Pre-order an In-order sequence this level order sequence seems possible. [4, 1, 5, 2, 6, 3, 7, 8] . I don't know how. I am trying to figure this out. I am unable to construct BST in paper (drawing) that

How to traverse a B-Tree using Stack without recursion?

别说谁变了你拦得住时间么 提交于 2021-02-11 17:42:03
问题 How to traverse a B-Tree using Stack without recursion? This is function to traverse Btree using Stack but it does not work void StackTraverse( BTreeNode node01 ) { Stack< BTreeNode > stack01 = new Stack< BTreeNode >(); stack01.push( node01 ); // first node "to check" String string01 = ""; int i = 0; while ( stack01.size() > 0 ) { BTreeNode current = stack01.pop(); if ( current.leaf == false ) {// if node is valid for ( i = 0; i < current.n; i++ ) { string01 = string01 + "Node : " + current

How to traverse a B-Tree using Stack without recursion?

限于喜欢 提交于 2021-02-11 17:41:45
问题 How to traverse a B-Tree using Stack without recursion? This is function to traverse Btree using Stack but it does not work void StackTraverse( BTreeNode node01 ) { Stack< BTreeNode > stack01 = new Stack< BTreeNode >(); stack01.push( node01 ); // first node "to check" String string01 = ""; int i = 0; while ( stack01.size() > 0 ) { BTreeNode current = stack01.pop(); if ( current.leaf == false ) {// if node is valid for ( i = 0; i < current.n; i++ ) { string01 = string01 + "Node : " + current

Write an algorithm that compute the Euler's number until

喜你入骨 提交于 2021-02-11 15:44:05
问题 My professor from Algorithms course gave me the following homework: Write a C/C++ program that calculates the value of the Euler's number (e) with a given accuracy of eps > 0. Hint: The number e = 1 + 1/1! +1/2! + ... + 1 / n! + ... = 2.7172 ... can be calculated as the sum of elements of the sequence x_0, x_1, x_2, ..., where x_0 = 1, x_1 = 1+ 1/1 !, x_2 = 1 + 1/1! +1/2 !, ..., the summation continues as long as the condition |x_(i+1) - x_i| >= eps is valid. As he further explained, eps is

Write an algorithm that compute the Euler's number until

妖精的绣舞 提交于 2021-02-11 15:41:59
问题 My professor from Algorithms course gave me the following homework: Write a C/C++ program that calculates the value of the Euler's number (e) with a given accuracy of eps > 0. Hint: The number e = 1 + 1/1! +1/2! + ... + 1 / n! + ... = 2.7172 ... can be calculated as the sum of elements of the sequence x_0, x_1, x_2, ..., where x_0 = 1, x_1 = 1+ 1/1 !, x_2 = 1 + 1/1! +1/2 !, ..., the summation continues as long as the condition |x_(i+1) - x_i| >= eps is valid. As he further explained, eps is

Group Python lists based on repeated items

故事扮演 提交于 2021-02-11 15:37:46
问题 This question is very similar to this one Group Python list of lists into groups based on overlapping items, in fact it could be called a duplicate. Basically, I have a list of sub-lists where each sub-list contains some number of integers (this number is not the same among sub-lists). I need to group all sub-lists that share one integer or more. The reason I'm asking a new separate question is because I'm attempting to adapt Martijn Pieters' great answer with no luck. Here's the MWE: def

How to store timetables?

你离开我真会死。 提交于 2021-02-11 15:19:57
问题 I'm working on a project that must store employees' timetables. For example, one employee works Monday through Thursday from 8am to 2pm and from 4pm to 8pm. Another employee may work Tuesday through Saturday from 6am to 3pm. I'm looking for an algorithm or a method to store these kind of data in a MySQL database. These data will be rarely accessed so it's not important performance questions. I've thought to store it as a string but I don't know any algorithm to "encode" and "decode" this

Is there a more optimal solution to this money change problem?

旧巷老猫 提交于 2021-02-11 15:13:18
问题 I was asked to create a method that would: Return a Change object or null if there was no possible change The "machine" has unlimited bills of: 2, 5 and 10 The Change object must return the most minimal amount of bills possible This was a question asked in codingame.com and wanted to investigate further on it. Here's the code: package com; class Change { long coin2 = 0, bill5 = 0, bill10 = 0; } public class Test { static Change c = new Change(); public static void main(String[] args) { Change

Is there a more optimal solution to this money change problem?

耗尽温柔 提交于 2021-02-11 15:12:01
问题 I was asked to create a method that would: Return a Change object or null if there was no possible change The "machine" has unlimited bills of: 2, 5 and 10 The Change object must return the most minimal amount of bills possible This was a question asked in codingame.com and wanted to investigate further on it. Here's the code: package com; class Change { long coin2 = 0, bill5 = 0, bill10 = 0; } public class Test { static Change c = new Change(); public static void main(String[] args) { Change