Adjacency Matrix -> Directed graph -> DFS - java

This is the code my friends and I have come up with so far after fiddling around. What we are trying to do is read in the adjacency matrix (input.txt), then create a directed graph out of it so we can search it using Depth-First Search. We want the output of our program to provide the order of visited nodes.
The java code:
http://pastebin.com/bAzBadxi
The input.txt file:
http://pastebin.com/r72J34uA
My question is, what do we initialize "n" to? (line 32 in the java code)
Any help will be appreciated.

Create a vertex object before you use it.
Vertex n; // before g.addVertex(n);
I am not validating your algorithm, just removing compiler error, if your algo is correct it should work fine

What you are trying to solve is a problem of topological sorting.
Topological sorting
In this case, it doesn't matter what n you initialize to, you can simply use the first vertex in the adjacency matrix as the start.
And adjacency matrix (which should be a square matrix) is a legit representation of a directed graph, you can use the matrix to search the graph directly.

Related

java - How to find the largest weighted edge in a MST?

The question is quite simple as per the title.
I have an existing MST, undirected with weighted edges, with V vertexes. Given a starting node and ending node, is there an efficient algorithm that runs in O(V) time that returns the largest weight in the MST?
Thanks!
You can use modified BFS on your existing MST.
Maintain a global variable maxWeight, and as there won't be any loop here, you can get work done.
P.S. keep track of visited nodes as in BFS.

How to make a graph for braedth first search algorithm

I'm new to use graphs in Java, but I have implemented a code that finds the routes between two nodes using breadth first search algorithm and I need to show the output on a graph, can anyone help me in doing so .
For a beginner, I would recommend two popular graph representations:
Adjacency Matrix: https://en.wikipedia.org/wiki/Adjacency_matrix
Adjacency List: https://en.wikipedia.org/wiki/Adjacency_list
To understand and practice Breadth-first search/Depth-first search, use Adjacency Matrix because neighbors are easier to access in this data structure. It is as simple as a 2-dimension array:
int[][] adj = new int[10][20];
Example from Princeton University: http://algs4.cs.princeton.edu/41graph/AdjMatrixGraph.java.html

Best Way to Implement an Edge Weighted Graph in Java

First of all, I'm dealing with graphs more than 1000 edges and I'm traversing adjacency lists, as well as vertices more than 100 times per second. Therefore, I really need an efficient implementation that fits my goals.
My vertices are integers and my edges are undirected, weighted.
I've seen this code.
However, it models the adjacency lists using edge objects. Which means I have to spend O(|adj|) of time when I'd like to get the adjacents of a vertex, where |adj| is the cardinality of its adjacents.
On the other hand, I'm considering to model my adjacency lists using Map<Integer, Double>[] adj.
By using this method, I would just use adj[v], v being the vertex, and get the adjacents of the vertex to iterate over.
The other method requires something like:
public Set<Integer> adj(int v)
{
Set<Integer> adjacents = new HashSet<>();
for(Edge e: adj[v])
adjacents.add(e.other(v));
return adjacents;
}
My goals are:
I want to sort a subset of vertices by their connectivities (number of adjacents) any time I want.
Also, I need to sort the adjacents of a vertex, by the weights of the edges that connect itself and its neighbors.
I want to do these without using so much space that slows down the operations. Should I consider using an adjacency matrix?
I've used th JGrapht for a library for a variety of my own graph representations. They have a weighted graph implementation here: http://jgrapht.org/javadoc/org/jgrapht/graph/SimpleWeightedGraph.html
That seems to handle a lot of what you are looking for, and I've used it to represent graphs with up to around 2000 vertices, and it handles reasonably well for my needs, though I don't remember my access rate.

How do I implement a undirected graph for PPI using adjacency list in Perl or Java?

I've a list of proteins in a text file like the format below:
ATF-1 MET4
ATF-1 NFE2L1
ATF-2 ATF-7
ATF-2 B-ATF
ARR1 ARR1
ARR1 CHOP
I want to read from the text file and implement them in undirected graph using adjacency lists either in Java or in Perl. I want to calculate the minimum and maximum number of edges, the shortest and longest path between nodes, and other similar functions.
In perl, you can represent the graph using hash like this:
use warnings;
use strict;
my %graph;
sub add_edge {
my ($n1, $n2) = #_;
$graph{$n1}{$n2} = 1;
$graph{$n2}{$n1} = 1;
}
sub show_edges {
foreach my $n1 (keys %graph) {
foreach my $n2 (keys %{$graph{$n1}}) {
print "$n1 <-> $n2\n";
}
}
}
while (<>) {
my ($n1, $n2) = split /\s+/;
add_edge($n1, $n2);
}
show_edges();
Run it like this:
perl script.pl input.txt
For the shortest path you'll have to decide the start and end node that you want to search the shortest path for.
For this you can use Dijkstra's algorithm. In a nutshell this is how the algorithm works:
Let's call the start node A and the end node B.
Assume that we already know the shortest path for going from A to B. If we are at B, then backtracking our steps using the cheapest path should bring us back to point A. Dijkstra's algorithm starts at A and records the cost of path for going to all of A's adjacent nodes, and repeats the process for each of the adjacent nodes. Once done, then we can print the shortest path from A to B by backtracking from B to A.
To get the number of nodes: print keys %graph;
To get the number of edges you'll have to count (uniquely) the number of entries in each of the hash elements, for example to count the number of edges for one node: print keys %{$graph{'ATF-1'}};
Take a look at Open source libraries to design directed graphs. Their suggestion was to use JGraphT. The javadoc shows that they have implemented a wide range of graph operations, including shortest path.

Transposing on a Directed Graph

I've been assigned the following problem, but am having issues figuring it out. I know what I'd like to accomplish, but given the skeleton code he's outlined for us, I'm not sure where to start...I drew a pic to illustrate what I'd like to accomplish (I think...)
http://i802.photobucket.com/albums/yy304/Growler2009/Transposing-1.jpg
This is what I need to do:
Consider a directed graph G(V;A). The transpose of G written GT (V;AT ) is nothing more than
G where all the arcs have been transposed, i.e., the origin of the arc becomes the end and the end
becomes the origin. In a sense, GT is the \backward" version of G. For this question you must
implement an algorithm which, given a directed graph, produces its transpose. The API of the
algorithm is given by the following interface:
public interface Transpose<VT,AT> {
public DIGraph<VT,AT> doIt(DIGraph<VT,AT> src);
}
Implement the transpose algorithm given above. You have no restrictions on how to do this (except
that it must operate on a graph represented as an adjacency list and it cannot modify the original
graph. Report (in the comments) the space and time complexities in big O notation and brie
y
justify. (i.e., how long does it take and how much space does it uses to transpose a graph with n
vertices and m arcs).
Any help you can offer to get me started would be great.
Thanks!
in pseudolanguagecode:
create new empty set of edges E
for I in all edges:
(X,Y) = vertices of edge I ;
insert edge (Y,X) to E;
result is in E.
Space complexity: no requirements
Time complexity: O(num. of edges)

Categories

Resources