Find a path in an incidence matrix - java

In an incidence matrix we have:
e1 e2 e3 e4 e5 e6
v1 1 1 0 0 0 0
v2 0 0 1 1 0 1
v3 0 0 0 0 1 1
v4 1 0 1 0 0 0
v5 0 1 0 1 1 0
To find a path, we should find if the last vertex of and edge i is the start of another edge, and the last of the last edge is the first of the first edge.
Can someone help me to find a solution? I understand very well what it is but not how to implement it!

What do you determine as a path? Do you want to find out if it's possible to visit all node exactly once?
There are two general ways of searching a path: depth first or breadth first. If you have a look at some of the examples, it shouldn't be too hard to implement yourself.

Related

depth first search with 2D Array

I have an array like that :
0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 1 0 0 0 0
0 1 1 1 1 0 0 1 0 1 0
0 1 1 0 1 0 0 0 0 1 1
0 0 0 1 1 0 0 0 1 1 0
0 0 0 0 0 0 0 0 0 0 0
And I want to group elements which are '1'.
So you see I have a classical dfs by using a stack. The question is, If I have a matrix like the above, what is the time complexity of this algorithm where n is the number of matrix elements. (Row*column).If it worse than O(N) (since I must traverse whole 2D array) which approach will help me to improve this algorithm?
Example of an O(n * log n) algorithm
(where n is the number of matrix elements)
The idea of the algorithm is as follows.
Initialize a treeset of unhandled elements U by adding all matrix elements into it
While U isn't yet empty, take any u from U and check it for its value
If u = '0' then just remove it, i.e. U := U \ {u}
If u = '1' then start exploration DFS(u, U)
Where procedure DFS(u, U) uses the matrix to explore the '1' neighbours of u.
However, here comes the kicker, DFS(u, U) also removes every discovered element from U.
It is rather easy to understand and prove that this algorithm indeed always finishes in O(n * log n). Deleting an element from a treeset has worst-case complexity O(log n). Each DFS(u, U) run can visit at most |U| elements, and each element visited through any means is removed from U as the execution progresses. The algorithm terminates when U becomes empty.
Short summary
It is possible to produce an O(n^2) algorithm for example by running DFS on each element regardless of your previously attained knowledge.
Using any mechanism ensuring that you don't run DFS on an already discovered group/island is likely to produce a superior algorithm.
Sorry that I can't analyze your own algorithm directly, but this may help you do it on your own.
The lower bound of the algorithm that you need to devise should be O(m+n) - I am considering the number of rows and columns as m and n respectively. This is because you need to traverse the whole 2D array anyway. If you are solving this using two for loops, this will take O(m+n) as well. For each element in your matrix, you will compare with other 4 adjacent elements and hence, the overall checking size is <= 4mn.
I think there is no better way to solve this in less than O(m+n) time.
Please note that in your question, if you are considering m+n = N then the complexity that I am referring to is O(N).

Floyd -Warshall algorithm path between two vertexes (undirected graph)

I need your help with F-W algorithm. My question is: is it possible to find shortest path between two vertexes? Down below I will give you an example of what I want.
1 -----50(w)----- 5
| \__ / |
| \5(w) /20(w)|
10(w) \__3__/ |25(w)
| \___ |
| 18(w)\ |
2--------8(w)------4
If my picture isn't possible to see, here is a picture of it: http://prntscr.com/b6n43d
This is what I got with final cycle with weights:
0 10 5 18 25
10 0 15 8 33
5 15 0 18 20
18 8 18 0 25
25 33 20 25 0
And this is what I got with vertex'es:
0 1 2 1 2
0 0 0 3 3
0 0 0 3 4
1 1 2 0 4
2 3 2 3 0
And I need to find that vertex, that furthermost vertex be as most as possible near that first vertex. What I mean, that vertex 1 have 50 weight with 5 vertex (that mean they are furthermost from each other) and I need to find shortest path between 1 and 5. Any ideas how I can do that ?
The whole code were write'd from https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm pseuudo code.

Representing an adjacency matrix/list

I'm having a hard time thinking of an appropriate data structure to use to represent an adjacency matrix for an undirected graph.
I want to be able to take the nodes from these graphs and insert them into random positions in arrays, and then "score" the arrays based on how well they've managed to keep the adjacent nodes apart. i.e if node A and node B are connected in my graph, and the array places them next to each other, +1 would be added to the array's score, with the lowest scoring array being the best.
So what would be the best data structure to use to represent a collection of nodes, and the neighbouring nodes of each one in the collection?
If I understand your question which I do not think it really clear.
For an adjacency matrix, I think the best way to go is an Array. You can access each positions in O(1) and since it is an undirected graph, it should be easy to create. see graph below
0 --- 1------5---6
| \ \ | /
| \ \ | /
2 3----4---7
0 1 2 3 4 5 6 7
-----------------
0 | 0 1 1 1 0 0 0 0
1 | 1 0 0 0 1 1 0 0
2 | 1 0 0 0 0 0 0 0
3 | 1 0 0 0 1 0 0 0
4 | 0 1 0 1 0 0 0 1
5 | 0 1 0 0 0 0 1 1
6 | 0 0 0 0 0 1 0 1
7 | 0 0 0 0 1 1 1 0
------------------
You can implement your matrix like so and perform whatever operation you want on it. And all that matters is that if a location is not 0 then the graph is connected and you can just pick the highest value for whatever you are doing.

Creating a bipartite graph in java

I am new to programming and I need to create a bipartite graph from the matrix that i have. Most of the places that i have looked for has code for checking if the graph is bipartite or not. But none of them tell how to create it. Is there a way to do this in java?
the matrix is as follows:
A1 A2 A3
B1 0 1 1
B2 1 0 1
B3 0 0 1
B4 0 1 0

solving the eight queens to produce a solution

Below is my attempt at solving the 8 queens problem to print one solution. (place 8 queens on a chessboard such that none of them are attacking each other). However, this solution only places 6 queens. I need another opinion of where I am making a mistake. I'm doing this in more of a BFS style instead of backtracking.
It seems your algorithm is malfunctioning at some point. Upon running it, I found the following issues:
You are constantly setting visited[i][j] to 0 in your for loop in main. This always resets visited to 0 even if a recursion call is made. In fact, when you declare both visited and board they are initiated to arrays full of 0s. So you can get rid of both set statements in there. In addition, because you reset the arrays, your recursive function ends up setting both values to 0 and then finds them again."
For debugging, in the !hasQueen statement, you should output the board[row][col] coordinates, which show you the coordinates that have been found. The final list before it prints out the grid shows that 2,4 and 1,6 are found and set twice.
The actual chessboard that is output ends up with an impossible solution:
1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0
0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0
0 0 0 X 0 Y 0 0
0 0 0 Y 0 X 0 0
0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0
(sorry I can't get the numbers to format)
Both layout X and layout Y fail the 8 queens rules.
If you run your program with the setting to 0 commented out, you will see that it grinds to a halt after finding 6 locations.

Categories