Creating a bipartite graph in java - 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

Related

LIBSVM - perfect confusion matrix on training set, abysmal results on test set?

I am doing a project in argument mining. One of the tasks is classifying Strings as PREM(ise), CONC(lusion) or M(ajor)CONC(lusion). I am working with AAEC dataset and have a few thousand features per vector.
For the task I employ a CSVM with polynomial kernel implemented in LibSVM and accessed through WEKA.
I am performing a grid search (w/o cross-validation, its a custom code I wrote that trains an SVM on a subset of the data and prints its results) for best C, gamma. I am trying in range 10^-5 to 10^5 and 2^-15 to 2^3 respectively. I am also printing out the results on the training set and on the test set.
I either get all classified as a for both confusion matrices, or this :
Confusion matrix (on training set)
a b c <-- classified as
416 0 0 | a = PREM
8 169 0 | b = CONC
5 0 80 | c = MCONC
Confusion matrix (on test set)
a b c <-- classified as
107 1 0 | a = PREM
40 0 0 | b = CONC
16 0 0 | c = MCONC
I am not too familiar with SVMs and I am not sure whether this is supposed to be normal or anomalous. Intuitively it seems unlikely that the data is so well separable in the training set yet the result is completely off on the test set.
I am not sure how to proceed. Is this a result of not having optimal C,gamma or the data being not descriptive enough, or is this potentially a signal of a more hidden problem (e.g. filtering mistakes, overfitting)?
Advice would be appreciated, thanks!

Storing vertices in Java - Mapping Edges

I'm trying to build a graph with vertices, A, B, C & D. The graph must map an edge to a particular vertex if there is an overlap, For instance vertices A and C overlap as A has 1 -> 2, and C has 1 -> 4.
What would be an efficient way of storing these vertices then checking if their values overlap with one another?
Example:
A 1 2 3 4
B 9 10 12 13
C 1 4 2 3
D 15 16 17 18
It depends how often these graphs are expected to change. If it's just once off, storing the vertex constraint in a Map<Vertex, Set<Constraint>, calculating the intersections and then storing the final graph as an adjacency list or matrix is just fine.
You would do something like
For each vertex v:
For each other vertex u
if constraints(u) intersect constraints(v)
add edge between u and v
You will end up with a symmetric un-directed graph.

dynamic programming filling matrix in sequence alignment

hello guys i have 2d char array opt[][] and i have 2 sequence in my arrays like in example
my
`opt[0][0]=A
opt[0][1]=T
opt[0][2]=G
opt[0][3]=A`
and
opt[1][0]=A
opt[2][0]=G
opt[3][0]=C
opt[4][0]=T
i have this output currently
x/y| A T G A -
_______________________
0 A | 0 0 0 0
1 G | 0 0 0 0
2 C | 0 0 0 0
3 T | 0 0 0 0
4 - | 0 0 0 0
my problem is this how can i use dynamic programming
to create this array into this
http://i.stack.imgur.com/ViHc9.png
if its a match 0 penalty
if its a mismatch 1 penalty
if its a gap its 2 penalty
i can compare chars of my array like this
for(int i=0;i<4;i++){
if(opt[0][i]==opt[i+1][0]){
result[0][i] =1;
}
but this is just a simple test i made to see if i can compare and it turned out i can.
how can i go from here to there(to the picture array
I suggest you read these articles.
http://en.wikipedia.org/wiki/Smith%E2%80%93Waterman_algorithm
http://en.wikipedia.org/wiki/Needleman%E2%80%93Wunsch_algorithm
The implementation in any language is pretty trivial.
And if you need information about dynamic programming in general,
either Google for it yourself, or check these two links.
http://en.wikipedia.org/wiki/Dynamic_programming
https://www.topcoder.com/tc?d1=tutorials&d2=dynProg&module=Static

Find a path in an incidence matrix

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.

Finding a Minimum Spanning Tree from an Adjacency List where the Adjacency List is in a string array using Prims Algorithm

So I need some help coming up with a way to find a Minimum spanning tree.
Suppose I have my graph in the form of an adjacency list:
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
The first letter tells which node you are looking at, and the number tells how many connections to any other node there are. For example, A has two connections - one each to B and I. After that, the number that follows the letters simply tell the weight of an edge. B has weight 12 and I has weight 25. So I had originally planned to represent this entire thing as a String array
called Graph[8]. Each line would be a different slot in the array. I am having difficulties figuring out how to accomplish this with either Prims or Kruskalls algorithm.
This isn't a direct answer to your question per-say (seems like you're doing schoolwork), but I think it will help you get started. Why not create a data structure that more closely matches your mental model and build up from there?
class GraphNode {
final String name;
final List<GraphEdge> adjacentNodes;
public GraphNode(String name) {
this.name = name;
adjacentNodes = new ArrayList<GraphEdge>();
}
public void addAdjacency(GraphNode node, int weight) {
adjacentNodes.add(new GraphEdge(node, weight));
}
}
class GraphEdge {
final GraphNode node;
final int weight;
public GraphEdge(GraphEdge node, int weight) {
this.node = node;
this.weight = weight;
}
}
Suppose I have my tree in the form of an adjacency list
It is important (for your understanding) to note that you have a connected graph in this kind of an adjacency list, but I think it was just a typo. I will propose this as an edit, but I just want you to be aware of it.
The fact that it is a graph and not a tree can be seen from those lines:
A 2 B 12 I 25
B 3 C 10 H 40 I 8
Here you have a circle already at A-B-I:
A
12/_\25
B 8 I
Having a circle by definition means it cannot be a tree!
There is one more thing one can see from the way I presented this subgraph:
the edges have weights, not the nodes.
Now let's take a look at your provided example:
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
First of all we can see that this adjacency list is either incorrect or already optimized for your needs. This can (for example) be seen from the lines
E 2 F 60 G 38
F 0
The first line here shows an edge from E to F, yet the second line says F had a degree of 0, but the degree is defined by the edges incident to it!
This is what the adjacency list would look like if it was 'complete':
A 2 B 12 I 25
B 4 A 12 C 10 H 40 I 8
C 3 B 10 D 18 G 55
D 2 C 18 E 44
E 3 D 44 F 60 G 38
F 1 E 60
G 3 C 55 E 38 H 35
H 3 B 40 G 35 I 35
We can see lots of redundancy because every edge occurs twice, this is why your 'optimized' adjacency list better suits your needs - would it be this 'complete' example one would do many useless checks.
However, you should only rely on this if you can be sure that all data given to your code is already 'optimized' (or rather in this format)! I will take this as a given from now on.
Let's talk about your data structure.
You may of course use your proposed array of strings, but to be honest, i would rather recommend something like #AmirAfghani's proposed data structure. Using his approach would make your work easier (as it - as he already pointed out - would be closer to your mental representation) and even more efficient (i guess, don't rely on this guess ;)) as you would be doing many operations on strings otherwise.
In the title you asked after Prim's algorithm, but in your actual question you said either Prim's or Kruskal's. I will go with Kruskal simply because his algorithm is way easier and you seem to accept both.
Kruskal's algorithm
Kruskal's algorithm is fairly simple, it's basically:
Start with every node, but no edges
Repeat the following as often as possible:
From all of the unused/unchosen edges choose the one with the fewest weight (if there are more than one: just pick one of them)
Check if this edge would cause a circle, if it does, mark it as chosen and 'discard' it.
If it doesn't cause a circle, use it and mark it as used/chosen.
That's all. It's really that simple.
However i would like to mention one thing at this point, i think it best fits here: there is no "the minimum spanning tree" in general, but "a minimum spanning tree" as there can be many, so your results may vary!
Back to your data structure! You may now see why it would be a bad idea to use an array of strings as a data structure. You would repeat many operations on strings (for example checking the weight of every edge) and strings are immutable, that means you cannot simply 'throw away' one of the edges or mark one of the edges in whatever way.
Using a different approach you could just set the weight to -1, or even remove the entry for the edge!
Depth-First Search
From what I have seen I think your main problem is deciding whether an edge would cause a circle or not, right?
To decide this, you will probably have to call a Depth-First Search algorithm and use its return value.
The basic idea is this: start from one of the nodes (i will call this node the root) and try to find a way to the node at the other end of the chosen edge (i will call this node the target node). (of course in the graph which doesn't have this edge yet)
Choose one unvisited edge incident to your root
Mark this chosen edge as visited (or something like that, whatever you like)
repeat the last two steps for the node on the other side of the visited edge
whenever there are no unvisited edges, go back one edge
if you are back at your root and have no unvisited edges incident to it remaining, you are done. return false.
if you at any point visit your target node, you are done. return true.
now, if this method returns true this edge would cause a circle.

Categories