Depth First Search Example - graph

I have a Depth First Search example for a practice exam and I have asked another question regarding it and I think I have some of the concepts of it down...
I just want to confirm the results I got are correct or not, and if you can guide me what I did wrong and how I can fix it.
Here is a picture of it:
http://i.imgur.com/FdKxIUw.png
The results I got for DFI in order from 0 - 8 are :
1 5 7 6 2 3 4 8 9
For parent, I got confused because it starts at 0, the parent of 0 is 4? and the parent of 4 is 5? I am confused with parent, so if someone can clarify that my results for DFI are correct / wrong , and also help me with Parent, I would GREATLY appreciate it. This is all practice review for an exam.
Thanks everyone.

If you are doing a DFS on this graph starting from 0, then one example of the order you will visit the vertices is
{0, 4, 5, 6, 1, 3, 7, 8, 2}. The parent of each of the vertices from the given order (as represented by (vertex, parent) pairs) would be {(0,-)(4,0),(5,4),(6,0),(1,6),(3,1),(7,3),(8,3),(2,6)}
Hope this helps!

DFI looks ok to me.
The DFI tells you the order in which the nodes are found. Does not necessarily give you the information on parent.
The node you start from should not have any parent, since that is the root of your DFS tree. Have a look at the DFI, 0 is visited before any other node. That is why it has 1 in DFI.
Then next is 4, which has DFI 2. Since 4 is found from 0, its parent is 0. Then you found 5 from 4, so 5 should have parent 4. Then you traced back to 0. And so on for the rest of the graph.

Related

Setting up linear program for allocation/assignment problem

I have some troubles regarding a linear program I alreday solved and use excel but now i want to do it in r/python beacuse I already reach excels and the solvers limits. Therefore I am asking for help on this specific topic.
I tried it with the lPsovle package by also altering the lp.assign function but I cannot come up with an solution.
The problem is as follows:
Let's say I am a deliverer of an commodity good.
I have differnet depots which serve different areas. These areas MUST be served with their demands.
My depots on the other hand, have a constraint regarding their capacity what they can handle and deliver.
One depot can serve several areas, but one area can only be served by one depot.
I have the distance/cost matrix for the connections between depots and areas as well as the demand for that areas.
The objective for this solution should be that the areas should be served with the minimal possible effort.
Lets say the cost/distance matrix looks something like this:
assign.costs <- matrix (c(2, 7, 7, 2, 7, 7, 3, 2, 7, 2, 8, 10, 1, 9, 8, 2,7,8,9,10), 4, 10)
So this creates my matrix, with the costumers/areas in the first row/header and the depots in the first column/row names.
Now the demand of the areas/customers is:
assign.demand <- matrix (c(1,2,3,4,5,6,7,8,9,10), 1, 10)
The capacity restrictions, what amount the depos are able to serve is:
assign.capacity <- matrix (c(15,15,15,15), 4, 1)
So now i woukd like this problem to be solved by a lp to generate the allocation, which area should be served by which depot according to these restrictions.
The result should look something like this:
assign.solution <- matrix (c(1,0,0,0 ,0,1,0,0, 1,0,0,0, 1,0,0,0 ,0,0,0,1), 4, 10)
As for the restrictions this means that every column must some up to one.
I tried it with the lpsolve and lp.assign functions from lpSolve but I dont know exactly how to implement that exact kind of restrictions I have and i already tried to alter the lp.assign functions with no success.
If it helps, i can also formulate the equations for the lp.
Thank you all for your help, I am really stuck right now :D
BR
Step 1. Develop a mathematical model
The mathematical model can look like:
The blue entries represent data and the red ones indicate a decision variable. i are the depots and j are the customers. Ship indicates if we ship from i to j (it is a binary variable). The first constraint says that total amount shipped from depot i should not exceed its capacity. The second constraint says that there must be exactly one supplier i for each customer j.
Step 2. Implementation
This is now just a question of being precise. I follow the model from the previous section as closely as I can.
library(dplyr)
library(tidyr)
library(ROI)
library(ROI.plugin.symphony)
library(ompr)
library(ompr.roi)
num_depots <- 4
num_cust <- 10
cost <- matrix(c(2, 7, 7, 2, 7, 7, 3, 2, 7, 2, 8, 10, 1, 9, 8, 2,7,8,9,10), num_depots, num_cust)
demand <- c(1,2,3,4,5,6,7,8,9,10)
capacity <- c(15,15,15,15)
m <- MIPModel() %>%
add_variable(ship[i,j], i=1:num_depots, j=1:num_cust, type="binary") %>%
add_constraint(sum_expr(demand[j]*ship[i,j], j=1:num_cust) <= capacity[i], i=1:num_depots) %>%
add_constraint(sum_expr(ship[i,j], i=1:num_depots) == 1, j=1:num_cust) %>%
set_objective(sum_expr(cost[i,j]*ship[i,j], i=1:num_depots, j=1:num_cust),"min") %>%
solve_model(with_ROI(solver = "symphony", verbosity=1))
cat("Status:",solver_status(m),"\n")
cat("Objective:",objective_value(m),"\n")
get_solution(m,ship[i, j]) %>%
filter(value > 0)
We see how important it is to first write down a mathematical model. It is much more compact and easier to reason about than a bunch of code. Going directly to code often leads to all kind of problems. Like building a house without a blueprint. Even for this small example, writing down the mathematical model is a useful exercise.
For the implementation I used OMPR instead of the LpSolve package because OMPR allows me to stay closer to the mathematical model. LpSolve has a matrix interface, which is very difficult to use except for very structured models.
Step 3: Solve it
Status: optimal
Objective: 32
variable i j value
1 ship 1 1 1
2 ship 4 2 1
3 ship 2 3 1
4 ship 1 4 1
5 ship 3 5 1
6 ship 4 6 1
7 ship 4 7 1
8 ship 2 8 1
9 ship 1 9 1
10 ship 3 10 1
I believe this is the correct solution.

MS Access 3 conditions in a query?

I have a total column that ranges from -7 to 7. I want to recode them in a grand total variable so everything under 0 is now 0, 1 stays 1, 2 stays 2, and 3 and everything above just stays 3. I am stuck on how to include a third condition.
This is the code I have which makes everything above 3 a 3, and keeps 1's and 2's as is. But I do not know how to make all negative numbers become a 0.
GrandTotal: IIf([total]>=3,3,[total])
chained condition, try this:
GrandTotal: IIf([total]<0;0;IIf([total]>=3;3;[total]))

Figuring out how to work out n-turple questions?

Just as a side note, this isn't asking someone to help with homework it's just a example question in my lecture notes that i'm not grasping and would greatly appreciate if someone could explain to me how exactly to work it out.
It says simply this:
Let G = {3,5,7}. Write down some examples of 4-tuples.
Thank you to anyone who tries to help, this is mathematics to understand a systems unit :)
Your collection G is a set, which is unordered and non-repeating, containing three elements. You want some 4-tuples, which is an ordered collection of possibly-repeating elements, and there must be 4 elements.
We show tuples by using parentheses around the collection, while a set like G is written using braces (curly brackets). Some examples of 4-tuples using the elements of G are
(3, 3, 3, 3)
(3, 3, 3, 5)
(3, 3, 3, 7)
(3, 3, 5, 3)
...
(7, 7, 7, 5)
(7, 7, 7, 7)
That list of mine was in a particular order, called the lexigraphical order. Since there are 4 elements and each element has 3 choices regardless of the other choices, the total number of 4-tuples is
3x3x3x3 = 81
As another answer implied, your question is somewhat ambiguous. I assumed that each 4-tuple was to have elements taken from your set G, but your question did not actually say that. It does seem to be implied, however.
A 4-tuple, in math, is any vector populated with 4 objects. An example of a 4-tuple is {1, 5, 3, 7}.
Without more context, I can't use the fact that G = {3, 5, 7}.

What data structure should be used while designing algo for multiplication and division problems?

Considering basic example of multiplication where 12*24 = 288. Now I am looking for single or multiple data structures where I can keep each an every information of the intermediate steps performed during multiplication. e.g. 2*4 fetches 8, 1*4 fetches 4, etc.
I need to store such intermediate information so as to facilitate me to tell user exactly where he went wrong in his operations.
http://tutrr.com
Focus on first on the capability you need to provide.
For example, the user will enter one digit of his answer, you need to check it and give feedback. For example in 28 x 57 assuming you are teaching traditional "long multiplication" then the user needs to mulitply 28 by 7, recording 6 in the units, carrying 5 and then 9, remembering to adding the carried 5 and then 1. suppose he enters 4 in the tens column, you might want to say "Yes, 7 x 2 is 14, but don't forget to add the 5 you carried"
So to support this you need functions such as
getCorrectWorkingDigit( int leftDigitIndex, int rightDigitIndex)
In this case we'd call
getCorrectWorrkingDigit( 1, 0 ) and get 9 as the answer
and
getWorkingCarryDigit( int leftDigitIndex, int rightDigitIndex)
so
getWorkingCarryDigit( 1, 0 ) and get 5 as the answer
You will need a few other such functions, including functions for the final answer's digits.
Now, what data structure would allow you to do this? Your requirement is to enable your functions to be implemented. Well clearly you could build some kind of array of objects, representing each Working position, and each position in the final answer. But I think that's overkill, you can implement those functions directly against the question. All you actually need are the two integers (28 and 57 in my example) you can compute the function values on the fly, no need for keeping the target.
Having written all that I've just realised that you probably also want to keep the values the user entered, and for that a data structure might be useful, keeping the individual digits will be convenient.
For and "row" of working, and for the final result, how about an array of digits, where the index corresponds to the power of 10, so represent 196 as
[6, 9, 1]
and for the working put that in a Set, keyed by the power of ten of the right digit. In my 28 x 57:
0 -> [6, 9, 1] // this is 7 x 28
10 -> [0, 4, 1] // this is 5 x 28

Exam question about hash tables (interpretation of wording)

I was confused about the wording of a particular exam question about hash tables. The way I understand it there could be two different answers depending on the interpretation. So I was wondering if someone could help determine which understanding is correct. The question is below:
We have a hash table of size 7 to store integer keys, with hash function h(x) = x mod 7. If we use linear probing and insert elements in the order 1, 15, 14, 3, 9, 5, 27, how many times will an element try to move to an occupied spot?
I'll break down my two different understandings of this question. First of all the initial indexes of each element would be:
1: 1
15: 1
14: 0
3: 3
9: 2
5: 5
27: 6
First interpretation:
1: is inserted into index 1
15: tries to go to index 1, but due to a collision moves left to index 0. Collision count = 1
14: tries to go to index 0, but due to collision moves left to index 6. Collision count = 2
3: is inserted into index 3
9: is inserted into index 2
5: is inserted into index 5
27: tries to go to index 6, but due to collisions moves to index 5 and then to 4 which is empty. Collision count = 4
Answer: 4?
Second interpretation:
Only count the time when 27 tries to move to the occupied index 5 because of a collision with the element in index 6.
Answer: 1?
Which answer would be correct?
Thanks.
The wording is silly.
The teacher arguably wants #1 but I would argue that #2 is pedantically correct because an element will only ever try to move to an occupied spot once, as pointed out. In the other cases it does not move to an occupied spot but rather from an occupied spot to a free spot.
Tests in school are sort of silly -- the teacher (or TA) already knows what he/she wants. There is a line to draw between "being pedantically correct" and "giving the teacher what they want". (Just never, ever give in to the provably wrong!)
One thing that has never (at least that I recall ;-) failed me in a test or homework is providing an answer with a solid -- and correct -- justification for the answer; this may include also explaining the "other" answer.
Teacher/environment, repertoire, hubris and grade (to name a few) need to be balanced.
Happy schooling.
Interpretation 1 is correct. Collision with 6 means that slot 6 is occupied, so why don't you count it?

Resources