In the graph theory, is a strongly connected component SCC form a DAG? - directed-acyclic-graphs

I was trying to solve a problem to design an algorithm to determine whether a direct graph is semi-connected. Someone says it can be done by using topological sort every SCC in the graph. And SCC is guaranteed to be DAG. However, I think SCC graph must be a circle, why it is a DAG since DAG mean no circle.

You misunderstood the argument.
Suppose you have a graph that has points
A1 <--> A2 <--> A3 --> B1 <--> B2 --> C1 <--> C2
and A1 A2 A3, B1 B2, C1, C2 are SCC.
Then you treat A1 A2 A3 as a single point A. Any node connecting to one of A1 A2 A3 is treated as connecting to A, Any node connected from one of A1 A2 A3 is treated as connected from A. Same for merging points to B, C
So it became A --> B --> C. It is guaranteed that this is a DAG.

Related

Need Time efficient data structure to store computed value using values from different maps that have intersecting key type but same value type

I have a problem
I have 4 classes
Need to maintain 3 maps
class A
{
};
class B
{
};
class D
{
int d1;
int d2;
};
std::unordered_map<A, D> m1;
std::unordered_map<B, D> m2;
std::unordered_map<std::pair<A, B>, D> m3;
Note: These maps are not updated frequently.
Problem
1. For object of A: a, object of B: b, and object of pair of object of a and object of b,
2. Need to get an object of D: d such that
d.d1 = std::min( m1[a].d1, m2[b].d1, m3[std::pair(a,b)].d1 );
d.d2 = std::min( m1[a].d2, m2[b].d2, m3[std::pair(a,b)].d2 );
Note: The values may not exist in which case they will be skipped, eg: m1[a] does not exist, for a pair(a, b), than min( m2[b], m3[std::pair(a,b)] ) will be used
The simple solution is
1. find
m1[a], m2[b], m3[pair(a, b)]
three look-ups
2. perform minimum on d1 and d2
3. Get d
I can not save every b for every a, as the number of possible keys of A and B is huge
Question
1. I need to store this info in such a structure such that for a pair of a and b, I can get d, in less than three look-ups, I need to reduce the time for look-ups to the minimum while the space may be increased.
2. Also, the time of updating that structure can be more than updating the three maps.
3. If anyone knows any such data structure, please provide the name or the link or some explanation. Thanks.
Regards

Conditional assignment in c++ opencv

In Python with opencv/numpy (2D) arrays A1, A2, B1, B2 I can do
A1[B1>B2] = A2[B1>B2]
That is, I can assign values to A1 from A2 in places based on a condition between the B arrays.
How can I do this in opencv in C++?
Is the fastest way to simply loop over the arrays, or is there a better way?
Thank you very much in advance.
Use mask.
Mat m=B1>B2;
A2.copyTo(A1,m);

How should I multiply two matrices in C++?

Note: I'm using the standard library for C++, and am storing matrices as multidimensional vectors (see example below).
I'm having trouble finding the proper function to multiply two matrices in C++. To clarify what I'm trying to do:
A = | a1 a2 | B = | b1 |
| a3 a4 | | b2 |
Result = | (a1 * b1 + a2 * b2) |
| (a3 * b1 + a4 * b2) |
Obviously I could do this using a simple algorithm but I'm trying to find if there's a function to do this.
My specific example in C++:
#include <vector>
std::vector<std::vector<double>> A;
A.push_back({ 0.96, 0.56 });
A.push_back({ 0.01, 0.41 });
std::vector<std::vector<double>> B;
B.push_back({ 1.331749 });
B.push_back({ 1.0440705 });
Result = (A * B);
Where "Result" would be:
| 1.8631586 |
| 0.4413864 |
How should I go about doing the above?
No, there are no functions in the C++ library for vector multiplication. There are some building blocks, like std::for_each, std::copy, and std::transform, that can be used to implement them, but it's up to you implement the complete algorithm by yourself.
There might be some libraries floating around, somewhere on the Intertubes, that implement these algorithms. You can try your luck with Google.
I do not believe there is a function to multiply vectors together like matrices, however it would not be that difficult to implement on your own. I would suggest creating a matrix class. With this you could overload the * operator. This would look like the following:
class Matrix
{
private:
// Whatever implementation you choose.
// This would probably be done with a vector of vectors.
public:
Matrix& operator*(const Matrix&) const;
// Public interface
}
This would allow you to develop a simple algorithm for multiplying two matrices.

using prefetch_related to get only a particular entry of a relation

Assume the following models
Class A(): pass
Class B():
i = integer()
aa = foreignkey('A', related_name = 'fka')
For simplicity assume the below entries,
A() - a1 -> pk = 1
B() - b1 -> i = 1, aa = a1
B() - b2 -> i = 2, aa = a1
B() - b3 -> i = 3, aa = a1
B() - b4 -> i = 4, aa = a1
B() - b5 -> i = 5, aa = a1
I know,
foo = A.objects.get(pk = 1).prefetch_related('fka')
will give me the entries b1, b2, b3, b4 and b5.
But what I want to know is, is it possible to alter this query in anyway to get only a particular entry of B() associated with A().
Suppose I want to prefetch only entry of 'B', with 'i' as 2 (get b2, and skip b1, b3, b4 and b5).
If it is not possible to do it using the prefetch then, what is the best way to do it ?
Note: get(pk = 1) is used to keep the simplicity for explanation, but at that place usually there will be filter(**conditions).
Is it necessary to do the query on the A objects rather than the B objects? If you reverse what you do the additional lookup on, you could use select_related():
foo = B.objects.select_related('aa').get(i=2)
This has the advantage of reducing your number of database hits. Because prefetch_related() always does its own database lookup, your proposed scenario of using prefetch_related() isn't any more efficient, as written, than simply doing:
a = A.objects.get(pk=1)
b = a.fka.get(i=2)
That's not to say it wouldn't be more efficient in scenarios with more complex filtering, but select_related() uses a SQL join and therefore only hits the database once. That said, if you absolutely must lookup the A objects first, the best answer would be: upgrade to Django 1.7. The new Prefetch command in Django 1.7 allows for more complex operations on prefetch_related(), including filtering, by allowing you to specify custom querysets:
qs = B.objects.filter(i=2)
foo = A.objects.get(pk = 1).prefetch_related(Prefetch('fka',qs))

CODE1 at SPOJ - cannot solve it

I am trying to solve the problem Secret Code on SPOJ, and it's obviously a math problem.
The full problem
For those who are lazy to go and read, it's like this:
a0, a1, a2, ..., an - sequence of N numbers
B - a Complex Number (has both real and imaginary components)
X = a0 + a1*B + a2*(B^2) + a3*(B^3) + ... + an*(B^n)
So if you are given B and X, you should find a0, a1, ..an.
I don't know how or where to start, because not even N is known, just X and B.
The problem is not as easy as expressing a number in a base B, because B is a complex number.
How can it be solved?
The key is that a0 .. an are not arbitrary numbers, they're integers (otherwise, this wouldn't be possible in general). You're given the number X , and are asked to express it in base B. Why don't you start by working a few examples for a specific value of B?
If I asked you to write 17 in base 2, would you be able to do that? Can you find a way to generalize the algorithm you use to bases other than 2?