Algorithm to find a block in a 2D matrix - 2d

I want to find a block in a 2D matrix. Inside this matrix there's only one block of any size, or nothing. If there's a block, it will always be connected to two angles of the matrix. Let me show you a couple of valid samples.
----------------XXXX
----------------XXXX
----------------XXXX
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
Another one
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
-------------XXXXXXX
-------------XXXXXXX
-------------XXXXXXX
What's the most efficient algorithm to find the position/size of the block? I'm using a loop that traverses both arrays but it's slow as hell!

Related

how to find the complexity of T(n) = T(3n/4) + T(n/3) + n^2

I was asked to find the asymptotic complexity of the given function using recursion tree
but I'm struggling to find the correct complexity at each level
Let's draw out the first two levels of the recursion tree:
+------------------+
| Input size n |
| Work done: n^2 |
+------------------+
/ \
+--------------------+ +--------------------+
| Input size: 3n/4 | | Input size: n/3 |
| Work done: 9n^2/16 | | Work done: n^2/9 |
+--------------------+ +--------------------+
Once we've done that, let's sum up the work done by each layer. That top layer does n2 work. That next layer does
(9/16)n2 + (1/9)n2 = (43/48)n2
total work. Notice that the work done by this second level is (43/48)ths of the work done in the level just above it. If you expand out a few more levels of the recursion tree, you'll find that the next level does (43/48)2n2 work, the level below that does (43/48)3n2 work, and that more generally the work done by level l in the tree is (43/48)ln2. (Convince yourself of this - don't just take my word for it!)
From there, you can compute the total amount of work done by recursion tree by summing up the work done per level across all the levels of the tree. As a hint, you're looking at the sum of a geometric sequence that decays from one term to the next - does this remind you of any of the cases of the Master Theorem?

Pushdown Automata for the Language of Both Balanced Parentheses and Brackets

I need to figure out a pushdown automata for constructing strings of the language of both balanced parentheses and brackets, such as this ((([()))])()[]. It seems pretty easy to do for one type of parentheses; your stack consists of ( that you push when you see them, and then you pop one off for each ) you see. However, I'm having trouble figuring it out for the two types of parentheses. Does anyone have any suggestions? Thanks.
You are having trouble because the language you described is not context free (try writing it's context free grammar, it's impossible), because the Pumping Lemma doesn't hold for it.
Intuitively, a PDA can only "remember" one number at all times, while your language requires "remembering" the number of ( and the number of [ previously seen.
There is a subset of your language that is CF, the language of nested and balanced brackets and parentheses.
The CF grammar:
S -> B | P | ε
B -> [B] | [P] | ε
P -> (B) | (P) | ε
And the associated PDA:
when it sees [, pushes b
when it sees (, pushes p
when it sees ), if p is at the top of the stack it pops it, otherwise it rejects the word
when it sees ], if b is at the top of the stack it pops it, otherwise it rejects the word

OCaml - Mirror image of a tree

My tree type is
type 'a tree = Tree of 'a * 'a tree list;;
How can I get the mirror image of a tree like this? For me is confusing having a list of children, because I do not know how to get individually to each child and do the recursion whitout losing the parent's track, any idea?
EDITED:
I have kept trying and I think I got the solution:
let spec arbol =
match arbol with
Tree(a,[])-> Tree(a,[])
| Tree(a,l)-> Tree(a, List.rev (
let rec aux = function
Tree(a,[])::[]->Tree(a,[])::[]
| Tree(a,[])::l-> [Tree(a,[])]#(aux l)
| Tree(a,t)::[]-> [Tree(a, (List.rev (aux t)))]
| Tree(a,t)::l-> [Tree(a, (List.rev (aux t)))]#(aux l)
in aux l));;
I have tried it with this tree:
let p = Tree(1, [
Tree(2,[]);
Tree(3, [
Tree(6,[]);
Tree(7,[])
]);
Tree(4,[]);
Tree(5,[])
]);;
And I got as result of # spec p;;
-: int tree = Tree (1, [
Tree (5,[]);
Tree (4,[]);
Tree (3,[
Tree(7,[]);
Tree(6,[])]);
Tree (2,[])
])
So I guess my function works as expected. Let me know if it is not correct
If I understand the function you're trying to compute, there is a very simple answer that takes one line of code.
Unfortunately your code introduces so many cases that it's hard to check by eye.
It looks to me like your aux function is intended to calculate the mirror image of a list of trees. However, it doesn't work on an empty list. If you rewrite aux to work on an empty list, you might find that you won't require so many different cases. In particular, you could remove your outermost match and half the cases in your inner match.
In fact, your aux function (if correct) does all the work. If you look at it properly, you could just use aux for everything.
Since you're using List.rev, I assume you could also use List.map. That would be something to look at also.
Update
Trees are inherently recursive structures, so when looking for a tree algorithm, it often helps to imagine how you would use your algorithm recursively. In this case, you could ask yourself how you would put together mirror images of all the subtrees of a tree to make a mirror image of the whole tree.

create complex gremlin-java query

I have model implemented in titan graph database with relations presented below:
[A] ---(e1)---> [B] <---(e2)--- [C] ---(e3)---> [D]
| | | | | | |
prop:id | prop:number | | label:e3 |
| | prop:id |
label:e1 label:e2 prop:number
prop:prop1
A and B are "main vertices" (for example users), vertices B and C are "less important vertices" describing some data connected with users.
The input for the query algorithm is property id of vertex A.
I want to find all such vertices D, that are connected with A in the manner shown above. What's more I want to remember the property prop1 of the edge e1 between A and B.
More precisely, I want to efficiently retrieve pairs (prop1, numberD) where prop1 is the property of edge between A -> B (if the edge has this property), and numberD is the property number from D.
I don't know how to efficiently implement this query.
It is easy to retrieve only vertices D (using GremlinPipes):
pipe
.start(startVertex)
.outE("e1")
.inV().hasProperty("number")
.inE("e2")
.outV().hasProperty("id")
.outE("e3")
.inV().hasProperty("number");
But problems occur when I need to get also edges e1 and match them with vertices D.
I tried to compute all these steps separately, but is seems to be very inefficient.
Do you have any suggestions how to implement this (maybe using several queries) using gremlin-java or gremlin-groovy?
Thanks!
Take a look at the Pattern Match Pattern described here:
https://github.com/tinkerpop/gremlin/wiki/Pattern-Match-Pattern
startVertex.outE('e1').as('e')
.inV().hasProperty('number').inE("e2")
.outV().hasProperty("id")
.outE("e3")
.inV().hasProperty("number").as('d')
.table(t)
This should give an iterator of maps
[e:e1, d:D]
From each of these maps, you can easily extract the properties you are interested in.

finding the checksum

We have three 16-bit words:
0110011001100000
0101010101010101
1000111100001100
sum of the first two
0110011001100000
0101010101010101
-----------------
1011101110110101
adding the sum to the third
1000111100001100
1011101110110101
-------------------
10100101011000001
but the book says for that part that it's:
0100101011000010
It says that the last addition had overflow which was wrapped around but i don't understand.
After that it obtains the 1st complement:
1011010100111101
which becomes the checksum.
I don't understand the adding the sum to the third part. Can anyone explain?
Here's adding the sum to the third value.
Note the indentation. The overflow bit is the leftmost bit.
1000111100001100
1011101110110101
-----------------
10100101011000001
^
Add the overflow to the truncated result:
0100101011000001
0000000000000001
-----------------
0100101011000010
Which is the desired result for that step.

Resources