How to define adjacency multi-list in java? - java

I am trying to define adjacency multi-list in java. These type of lists are usually Edge based but I want to make it to be node/vertex based. Like list shows each vertex and edges associated with it.

I suggest you to watch the Coursera courses regarding algorithms, they are really well done and cover also these topics.
Here you can follow the Graph.java class for undirected graph with adjacency list
http://algs4.cs.princeton.edu/41undirected/Graph.java.html

Related

Roommate matching algorithm

I'm currently in an advanced data structure class and learned a good bit about the graph. For this summer, I was asked to help write an algorithm to match roommates. Now for my data structure class, I've written a City Path graph and performs some sorting and prims algorithms and I'm sort of thinking that a graph may be a great place to start with my roommate matching algorithm.
I was thinking that our data base could just be a text file, nothing too fancy. However I could initialize each nodes in the graph as a student each student would have an un-directed edge to many more students (no edge to the student who doesn't want to be roommate with another one, the sorority also doesn't want repeating roommate). Now I could also make the edge weights more, depending on the special interest.
Everything listed above is quite simple and I don't think I'll run into any problem implementing it. But here is my question:
How should I update the common interest field? Should I start that with a physical survey and then go back into the text file and update the weight of the edge manually? Or should I be creating a field that keeps track of the matching interests?
What you're trying to design is called bipartite matching. Fortunately unlike other bipartite matching algorithms, you won't need fancy graph algorithms and complex implementation for this. This is very close of Stable Marriage Problem and surprisingly there are very effective even easier algorithm for this.
If you are interested, I can share my C++ implementation of stable marriage problem.

Reading two points from dummy image and calculating distance using LinkedList C#/Java

I've got a general idea from teacher to use a LinkedList which stores Linkedlist. I've connected 3 different linkedlists to 1 main linkedlist showing sub routes on a map.
I'm stuck at connecting the child nodes of a linked list to the child of other linkedlist. Keep in mind that I've to find the distance between those routes. I've been thinking for a long time, had a search on google too but I found nothing. No idea this time in my mind for approaching near to the possible logic. The route is as follows.
http://imgur.com/a/zYGdu
The red ones are creating problem for me if I go later for finding the distance, How will I approach it?
Notice that the main linked list adds linkedlists in it as generic.
Is there any other approach which can be used. I'll use google maps for finding distance between these points one by one ad implementing it to each of the nodes. :( This is what comes in my mind.

K farthest points from vertex in graph edu.uci.ics.jung

I want to find K farthest points from a given vertex in a jung implementation of directed graph.
I assume that BFSDistanceLabeler does the job. However, it doesn't provide api for returning K farthest points, so I'll have to do it manually by traversing all vertices in graph and calling getDistance method. Or is there a better way?
But there is a bigger challenge for me. Despite the fact the graph is directed, I want to treat it as undirected for the distance labeler. Is it possible to somehow switch fast from from directed graph to its undirected version?
Why do I need to treat graph as undirected?
I analyze a very large network (milions of vertices) in consequent steps. In every step a small part of network (thousands of vertices) is loaded into a graph and analysed. This analysis requires directed graph and provides result for a one specific vertex which has to be located in the center of loaded area.
When I move from step A to step B, I could delete the whole previous graph and create a new one. Nevertheless, this would be very time consuming. Because I know my new vertex of interest is near to the previous one, a big part of graph can be reused.
And that is why I need to remove K farthest vertices for the new main vertex and replace them with new vertices from surrounding of this vertex.
Lets take a look at bottom picture with graph and lets say that vertex 1 is our vertex of interest. Since the graph is directed vertex number 6 is farthest. However, if graph was treated as undirected, then vertex number 4 would be the farthest and that is what i need.
There is not going to be an asymptotically faster way to find all the farthest vertices from a given input vertex than finding the distances to all vertices.
You can get the farthest vertices more efficiently by calling getVerticesInOrderVisited(), and then traversing the list in reverse order starting from the 'tail': this list should be populated in increasing order of distance from the root (set), so just get vertices from the list until the distance for each one starts decreasing.
(Note: this will not pick up the vertices that may be completely disconnected from the root vertex, which you may consider to be the "farthest"; getUnvisitedVertices() will do that.)
To answer your second question*: essentially what you want to do is to treat the directed edges as undirected. JUNG allows you to do this; instead of using getSuccessors()/getPredecessors(), you can call getNeighbors(), for instance.
As you've inferred, BFSDistanceLabeler doesn't do this; it wants to respect the edge direction if it exists, so it uses getSuccessors().
So here are some options:
Use jung.algorithms.transformation.DirectionTransformer.toUndirected(). This is very easy, but involves creating a bunch of new (undirected) edges
Create a subclass of BFSDistanceLabeler that overrides labelDistances(), and replaces getSuccessors() with getNeighbors(). This is pretty easy, if not very elegant.
Create a GraphDecorator subclass that overrides getSuccessors() to call getNeighbors() on its delegate graph. Then create an instance of your subclass where your original graph is the delegate.
This is the most elegant and general solution. (And at some point it may be useful for us to provide utilities that do this for you; please feel free to file an issue on the JUNG GitHub page: https://github.com/jrtom/jung/issues)
Hope this helps.
*For future reference, please split unrelated questions into separate StackOverflow questions; it makes them easier to answer and to find.

In memory weighted graph for a linked data (RDF)

It is a subset of dbpedia dataset which needs to be loaded in memory.
Operation will mainly involve traversing the graph based on edge weights in both directions
3 types of queries
Finding Path between 2 nodes
Given Subject and Predicate find object.
Given Object and Predicate find subject.
It is a sparse Graph
Performance w.r.t time is important
Implementation Language: Java
Use of weights: Filtering. The edges with weight higher than some threshold will be selected.
As it will be a sparse graph, using the Matrix data structure will be HIGHLY space inefficient.
Initial idea was to make java-objects for every subject and let each subject store a 2d array of 3 columns for predicate, object and weight.
But with this kind of data structure it will be hard to generate a list of subjects given an object. Even though it is good with generating the objects given subject.
Is there any proper(more time efficient) way to achieve this? Which could make traversals in both directions easier.
The graph in question is Categories(SKOS)

how to find if nodes are connected on a edge-weighted graph using adjacency matrix

I have a project that reads from an external file some statements and stores them into an weighted-edge directed graph using adjacency matrix. For example: A has B, C and D, B plus C, A and D, etc. When the program in the beginning reads the statements from the file, they are stored into two ArrayLists in the main class, one for the nodes and one for the edges. After some operations, all is added into the graph.
The problem I have now if how to check if the nodes in the graph are connected and if they are, how to display them, with their weights, in the main class. I heard that I can use Breadth-First Search or Depth-First Search but do not know how to implement them with weighted-edges and with nodes and edges that are made out of Strings and not Integers.
The weighted version of BFS is called Dijkstra's algorithm. It is supposed to find the shortest paths from the origin to all other vertices. Pseudo code is given in wikipedia page and you can implement it from given pseudo code which will be much easier.

Categories