4

I've a directed unweighted graph. Number of nodes and all links between nodes are given. I tried to do the task with array of vectors but java doesn't support it. ArrayList and Vectors support random access iterators but unable to do it in java as I'm new to it. I don't want to use 2-Dimensional matrix for it. I want to implement it as an array of N given nodes, where each node has a list of those nodes which are connected to it. Please somebody provide a pseudocode or anything which could help me. For Example, a graph is given as

5
3 4
4 2
1 5
4 3
1 3
2 5

here 5 nodes numbered 1 to 5 are given. Following are the directed edges from first node to second node. I want to represent it as adjacency list of graph. Could anybody give the implementation of it?

Bharat Kul Ratan
  • 985
  • 2
  • 12
  • 24
  • 2
    You may wish to look at the related content on stackoverflow that is listed in a column on the bottom right of this page. Also, if you are hindered by lack of knowledge of a Java library, such as the Collections library (ArrayLists, Vectors,...), by all means Google for some tutorials so you can use these useful tools. Also the related content links will tell you about 3rd party libraries that are built specifically for graph creation and manipulation. – Hovercraft Full Of Eels Oct 22 '11 at 12:09
  • I've gone search this on google as well as on this site but I couldn't find. At last I've to ask my own question. Also I don't want to use 3rd party libraries. – Bharat Kul Ratan Oct 22 '11 at 12:12
  • Again, the related content will be useful to you, even if to just to let you know how to started and what knowledge base you'll need to gain. Even if you still can't figure out the assignment, the information will at least help you write a more knowledgeable and specific question. – Hovercraft Full Of Eels Oct 22 '11 at 12:21

3 Answers3

5

An adjacency list such as Map<Node, List<Node>> or List<List<Node>> may be suitable.

Addendum: In using Java Collections, it may be helpful to note that Map and List are interfaces that provide characteristic methods, while you may want to choose specific implementations based on the requirements of the algorithms you want to implement using your data structure.

Addendum: There's a related example here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

You could use many collection data structures, in particular hash tables or sets, for your purpose. Java gives you a lot of collection generic containers (HashMap-s, ArrayList-s etc etc.). I'm not a Java expert, but searching for Java Collections give a big lot of results, e.g. this tutorial

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

Too bad that you are asking for an implementation of a directed unweighted graph rather than for a straight-use. Otherwise, I will suggest you to use a readily-made framework for almost anything that relates to network/graph called JUNG2. You can use it either in GUI or in non-GUI mode. It will save you a lot of time tinkering. Following is its tutorial link:

http://www.grotto-networking.com/JUNG/JUNG2-Tutorial.pdf

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
ecle
  • 3,952
  • 1
  • 18
  • 22