Lets say I have a graph of x nodes. I want to first represent it and then with help of a Algorithm Y, I would assign value to each of the node. Then I want to refresh the graph to display the value calculated.
Steps
1) Represent the graph as 2d Matrix. Perform processing over the 2d Matrix and then use the result to generate a new graph. [Better than doing processing by iterating the graph]
Problems:
1) I need to create a 2d Array with indexes as node names i.e string. I am not sure what's the best way to do this?
2) What's the best graph api which can A) produce good looking results 2) user friendly 3) allows vertex names as strings
I hope I made myself clear. Any input would be of immense help.
Thanks
Sunil
A Java array can't use strings as indexes, only non-negative integers. I think what you want is a java.util.Map. To get a two-dimensional structure, you probably want to create an array of Maps, or a Map that contains arrays, or something like that.
1) I need to create a 2d Array with indexes as node names i.e string. I am not sure what's the best way to do this?
Instead of using node names as indexes, you can arbitrarily label each node with an integer and use this as the index into the array. This is generally how adjancy matrices are implemented.
For example, if you have a graph of nodes A, B, C, D, E, and F, choose an arbitrary labeling like
0 -> A
1 -> B
2 -> C
3 -> D
4 -> E
5 -> F
You can store this in a Map<Integer, String> (or the reverse) if need be.
Then whenever there is an edge between two vertices, add a 1 (or the weight of the edge, if it is a weighted graph) to the matrix at that location.
Related
I want to use a singly-linked list to store an image consisting of RGB pixels. The node in the list should contain the value of RGB and its consecutively occurring times. For example, I have an image including 4 pixels and their (R,G,B) values are (8,2,5),(8,2,6),(8,7,6) and (8,7,9) respectively. In this situation, if using a singly-linked list to store them more compactly, the nodes should store the following information:
red needs one node: value 8, repetition times 4(because there are four consecutive 8).
green needs two nodes: 1st node(value:2, repetition 2), 2nd node(value:7,repetition 2)
blue needs three nodes: 1st node(value 5, repetition 1), 2nd node(value:6,repetition 2) 3rd node(value 8,repetition 1)
I wonder which of the following is better:
1.Using one Singly-linked list which contains 3 heads to point to R,G and B nodes respectively. That means storing R,G and B are stored in 3 different sequence of nodes.
2.Using one Singly-linked list which contains only 1 head, but the content of nodes has to be an array to store RGB. That means R,G,and B are combined as an array of 3 elements(R,G,B) and the arrays are stored in one single sequence of nodes.
I think the first solution is easier to realize, but it seems to require more space(creating more nodes). The second may have a potential problem since the number of nodes needed for R, G and B is not the same.
If your goal is to compress an image, you should really consider standard compression techniques. I have a feeling compressing and decompressing an image in the manner will not be very effecient.
Suppose we have integers as following:
(1, 2)=10,
(2,3)=20,
(3, 4)=30
etc…
These integers are distributed in a matrix with 2000*2000 dimensions.
But I just need 4000-6000 elements of this matrix with their indexes (i.e their position (i, j)). Other elements in matrix are zero. So I don’t need them.
Is there a good way other than using two dimensional Array/ArrayList, for saving these elements with their indexes, since we can call and reuse the elements and their indexes easily?
Using Array/ArrayList consume alot of memory.
Having a two-dimensional array of this size would be a waste of memory, because it would have 4,000,000 elements. Because you only need 4000-6000 elements, this array would be quite sparse.
You can use a HashMap that will map a Point to an Integer.
Map<Point, Integer> points = new HashMap<>();
You'll need to make sure you have a Point class that overrides hashcode and equals properly.
The HashMap would store only the 4000-6000 points that you put into it.
Solution with HashMap is pretty good and clean, but if you need linear algebra algorithms it'd be unwise.
You could use la4j, pure-java open-source linear algebra library. It contains sparse matrix implementations like Compressed Row Storage and many matrix algorithms like ranking, decomposition, linear system solving, whatever.
When representing graphs in memory in a language like Java, either an adjacency matrix is used (for dense graphs) or an adjacency list for sparse graphs.
So say we represent the latter like
Map<Integer, LinkedList<Integer>> graph;
The integer key represents the vertex and LinkedList contains all the other vertexes it points to.
Why use a LinkedList to represent the edges? Couldn't an int[] or ArrayList work just as fine, or is there a reason why you want to represent the edges in a way that maintains the ordering such as
2 -> 4 -> 1 -> 5
Either an int[] or ArrayList could also work.
I wouldn't recommend an int[] right off the bat though, as you'll need to cater for resizing in case you don't know all the sizes from the start, essentially simulating the ArrayList functionality, but it might make sense if memory is an issue.
A LinkedList might be slightly preferable since you'd need to either make the array / ArrayList large enough to handle the maximum number of possible edges, or resize it as you go, where-as you don't have this problem with a LinkedList, but then again, creating the graph probably isn't the most resource-intensive task for most applications.
Bottom line - it's most likely going to make a negligible difference for most applications - just pick whichever one you feel most comfortable with (unless of course you need to do access-by-index a lot, or something which one of the two performs a lot better than the other).
Algorithms 4th Edition by Sedgewick and Wayne proposes the following desired performance characteristics for a graph implementation that is useful for most graph-processing applications:
Space usage proportional to V + E
Constant time to add an edge
Time proportional to the degree of v to iterate through vertices adjacent to v (constant time per adjacent vertex processed)
Using a linked list to represent the vertices adjacent to each vertex has all these characteristics. Using an array instead of a linked list would result in either (1) or (2) not being achieved.
I am having trouble comprehending how to scan in an adjacency list to a graph. I understand how adjacency tables work and their mapping to each other but what I don't understand is what type of data type to store them in. My assignment is to take an input file that tells the number of vertexes G=(V,E) and gives the edges to the other numbers in the graph.
So for example:
3
010
101
110
so:
0 maps to 1
1 maps to 0
2 maps to 0
2 maps to 1
From there I have to implement a breath search and a depth search on them. Would a hash table be my best bet?
The difference of using BFS and DFS is in which data structure you store the data, one is a "queue" the other is a "stack" (your answer). If you use a java list, you could get them from the beginning or from the end, but you can also use "real" stack and queue.
So in your case, create a List, and store the origin of your search in it.
After a while loop, while you have elements in your list keep it going.
So pick your element from the list ( first or last) and evaluate if it is your target, if it is not, store all its neighbors in the list and keep it going.
You may add something two stop adding the same element twice, you should have a list of visited nodes.
But, I have doubts if you wanted to know where to store the adjacency list. An array of lists would do. Every vertex, vertex[i] has a List with all the vertices that is connected to.
I want to know how to implement a DFA as a linked list in C/C++/Java.
since every state can have several branches, you probably need more than one linked list. that means, every state has an array of n linked lists. so it's more like a tree structure with cycles than a simple linked list.
This is definitely possible, but would be grossly inefficient. What you would do is to simply store all your states in a link list, and then each state would need to keep a transition table. The transition table would look something like:
'a' -> 2
'b' -> 5
where your alphabet is {a,b}, and 2 and 5 are the states stored at position 2 and 5 in the linked list. As I said, this is definitely NOT how you would want to implement a DFA, but it is possible.
The first thing that came up in my mind is that,
create a class/struct called state with two array components. one for the states that can reach our state and one for the ones that are reachable from our state.
Then create a linked list whose elements are your states.
here's my implementation of this class
class state
{
private:
string stateName;
vector<state> states_before_me;
vector<state> states_after_me;
state* next;
//methods of this state
}
Single linked list couldn't represent the DFA efficiently. You can think DFA as a directed weighted graph data structure as states are vertices, transitions are edges, transition symbols are weights. There are two main method to implement graph structure.
i) Adjacency list: It basically has V(Number of vertices) linked lists. Each link list contains vertices which has edge to corresponding vertex. If we have vertices (1,2,3) and edges (1,2),(1,3),(2,1),(2,3),(3,3) corresponding adjanceny list is:
1->2->3
2->1->3
3->3
ii) Adjacency matrix: It is a VxV matrix with every entry at (i,j) symbolize an edge from i to j. The same example above represented like(1 means there is edge, 0 mean there is not):
1 2 3
1 0 1 1
2 1 0 1
3 0 0 1
But you must make little changes to these because your graph is weighted.
For list implementation you can change vertices in linklist to a struct which contains vertex and the weight of the edge connecting these vertices.
For matrix implementation you can place the weights directly into matrix instead of 0,1 values.
If you don't want to deal with the implementation of graph class there is libraries like Boost Graph Library which contains the two implementation and all the important graph algorithms DFS to Dijkstra's shortest path algorithm. You can look it up from http://www.boost.org/doc/libs/1_47_0/libs/graph/doc/index.html.