473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Need Help with implementing an adjacency list representation in java

thatos
105 100+
Here is the EdgeList class
Expand|Select|Wrap|Line Numbers
  1. class Graph {
  2.  
  3.     protected int numvertices;
  4.     protected int numedges;
  5.     protected boolean directed;
  6.     protected EdgeList adjlist [];
  7.  
  8.     // Constructors 
  9.  
  10.     public Graph() {};
  11.  
  12.     //Below constructor you specify the number of edges, 
  13.     //it assumes that the graph is undirected
  14.     public Graph(int n) {
  15.  numvertices = n;
  16.  directed=false;
  17.  adjlist = new EdgeList [n];
  18.  
  19.     }
  20.   //Below constructor you specify the number of edges and whether it is directed or not
  21.     public Graph(int n, boolean isdirected) {
  22.  numvertices = n;
  23.  directed=isdirected;
  24.  adjlist = new  EdgeList [n];
  25.     }
  26.  
  27.    //SimpleInp class is a short version of BufferedReader 
  28.     public void read(SimpleInp d) throws IOException {
  29.  String line, first, second; 
  30.  int x, y, split;   
  31.  
  32.  while (true) {
  33.      // get next line of input -- containts two ints split by a " "
  34.      line = d.readLine(); 
  35.      if (line == null) {break;  }  // Exit here
  36.      // find where the split is
  37.      split = line.indexOf(" ");
  38.      // extract out the numbers and convert to integer
  39.      first = line.substring(0,split);
  40.      second= line.substring(split+1);
  41.      x = Integer.parseInt(first);
  42.      y = Integer.parseInt(second);
  43.      addEdge(x,y); //It then adds that edge
  44.  }
  45.     }
  46.  
  47.  
  48.  
  49.  //Checks whether the gives edges are adjacent
  50.     public boolean isedge(int x, int y) {
  51.  EdgeList curr;
  52.  curr = adjlist[x];
  53.  while (curr != null) {
  54.      if (curr.y == y) return true;
  55.      curr = curr.next;
  56.  }
  57.  return false;
  58.     }
  59.  
  60.  
  61.  //Adds an edge to the graph
  62.     public void addEdge(int x, int y) {
  63.  
  64.  EdgeList curr = new EdgeList(x,y);
  65.  curr.next = adjlist[x];
  66.  adjlist[x] = curr;
  67.  
  68.  
  69.  if (!directed) 
  70.      {
  71.   curr = new EdgeList(y, x);
  72.   curr.next = adjlist[y];
  73.   adjlist[y] = curr;
  74.      }
  75.     }
  76.  
  77.  
  78.  //Removes a certain edge between x and y
  79.     private void cutEdge(int x, int y) {
  80.  EdgeList prev, curr;
  81.  prev = null;
  82.  curr = adjlist[x];
  83.  while (curr != null) {
  84.      if (curr.y == y) {
  85.   if (curr == adjlist [x] ) {
  86.       adjlist[x] = curr.next;
  87.   } else {
  88.       prev.next = curr.next;
  89.   }
  90.   break;
  91.      }
  92.      prev = curr;
  93.      curr = curr.next;
  94.  }
  95.     }
  96.  
  97.  
  98.  //Deletes an edge from a given graph
  99.     public void deleteEdge (int x, int y) {
  100.  cutEdge(x,y);
  101.  if (!directed) {cutEdge(y,x);};
  102.     }
  103.  
  104.  
  105.  
  106.     public int getNumVertices() { return numvertices; }
  107.  
  108.  
  109.  
  110.     public int getNumEdges() 
  111.     { return numedges; }
  112.  
  113.  
  114.    /*
  115.     public int getMaxDegVertex(int v) {
  116.  
  117.     }
  118.  */
  119.  
  120.     private void show_edges(int v) {      
  121.      for (EdgeList i:adjlist){
  122.       if(i.x == v)
  123.        System.out.println(i.y);      
  124.      }
  125.     }
  126.  
  127.  
  128.  
  129.  
  130.  
  131.     public void print() {
  132.  System.out.println("Graph has "+numvertices+" vertices");
  133.  System.out.println("Graph is " + (directed ? "directed" : "not directed"));
  134.  for (int i=0; i<numvertices; i++) { show_edges(i); }
  135.     }
  136. }
  137.  
I would like to change the addEdge method so that when I add an edge it goes to the end of the adjlist for example if I had the following edge say 0,1) when I add a new edge it will come after that one such that my new representation will be (0,1),(0,2) it does not come before it, I must use the copyEdgeList method

The above were written by Mr S. Hazelhurst
May 11 '08 #1
7 9195
JosAH
11,448 Expert 8TB
I would like to change the addEdge method so that when I add an edge it goes to the end of the adjlist for example if I had the following edge say 0,1) when I add a new edge it will come after that one such that my new representation will be (0,1),(0,2) it does not come before it, I must use the copyEdgeList method

The above were written by Mr S. Hazelhurst
I know you'd probably hate me for writing this but you can't expect us to write
your code for you; you want a new edge at the end of that list so you try to implement
it; only when you get stuck come back here; otherwise contact www.rentacoder.com
and hope for the best.

kind regards,

Jos
May 11 '08 #2
thatos
105 100+
I know you'd probably hate me for writing this but you can't expect us to write
your code for you; you want a new edge at the end of that list so you try to implement
it; only when you get stuck come back here; otherwise contact www.rentacoder.com
and hope for the best.

kind regards,

Jos
I don't expect you to write the code for me, I just nedd you to tell me steps how to go about solving, you can just write a pseudocode so that I can understand what have to do.
May 12 '08 #3
JosAH
11,448 Expert 8TB
I don't expect you to write the code for me, I just nedd you to tell me steps how to go about solving, you can just write a pseudocode so that I can understand what have to do.
There isn't much to tell: the current algorithm prepends an element at the front
of the list. You want it at the end so you have to loop over the list until you've
found the last one (next == null). There is one new condition: what to do if there
are no elements in the list yet. You can do that yourself; it's just simple list
fiddling. Make a small method for it.

kind regards,

Jos
May 12 '08 #4
thatos
105 100+
There isn't much to tell: the current algorithm prepends an element at the front
of the list. You want it at the end so you have to loop over the list until you've
found the last one (next == null). There is one new condition: what to do if there
are no elements in the list yet. You can do that yourself; it's just simple list
fiddling. Make a small method for it.

kind regards,

Jos
I ahd that idea, so how can I get to the last item where next == null since I have to go throw the list until I get that point remember i have to store those vertices which I have seen and go through I designed the following method, it only does this when edgelist has one element.
Expand|Select|Wrap|Line Numbers
  1. public void appendFront(EdgeList v,EdgeList w){
  2.     EdgeList curr = v.copyEdgeList();
  3.     while(curr != null){
  4.         curr = curr.next;
  5.         }
  6.     curr.next = w;
  7.     //this methods removes other element in the list
  8. }
  9.  
May 13 '08 #5
JosAH
11,448 Expert 8TB
I ahd that idea, so how can I get to the last item where next == null since I have to go throw the list until I get that point remember i have to store those vertices which I have seen and go through I designed the following method, it only does this when edgelist has one element.
Expand|Select|Wrap|Line Numbers
  1. public void appendFront(EdgeList v,EdgeList w){
  2.     EdgeList curr = v.copyEdgeList();
  3.     while(curr != null){
  4.         curr = curr.next;
  5.         }
  6.     curr.next = w;
  7.     //this methods removes other element in the list
  8. }
  9.  
A few remarks:

1) why is that method name 'appendFront' while you attempt to append it to the back of a list?
2) what does 'copyEdgeList()' do?
3) why are you passing an (unused) parameter 'v'?
4) you can be sure that curr == null when that while loop terminates so the next
statement will throw a NPE.

kind regards,

Jos
May 13 '08 #6
thatos
105 100+
A few remarks:

1) why is that method name 'appendFront' while you attempt to append it to the back of a list?
2) what does 'copyEdgeList()' do?
3) why are you passing an (unused) parameter 'v'?
4) you can be sure that curr == null when that while loop terminates so the next
statement will throw a NPE.

kind regards,

Jos
How can I reach that one where the is a null on the next by a loop
Please give me something cause I do not know what to do?
May 14 '08 #7
JosAH
11,448 Expert 8TB
How can I reach that one where the is a null on the next by a loop
Please give me something cause I do not know what to do?
Well, I'd do something like this:

Expand|Select|Wrap|Line Numbers
  1. while (curr != null && curr.next != null)
  2.    curr= curr.next;
  3.  
When that loop terminates either curr == null, indicating that there were no
elements in the list at all so you have to insert a first one, or curr != null so
you can simply append your new element to the end of the list.

kind regards,

Jos
May 14 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Herman | last post by:
Hi everyone, I'm implementing Dijkstra's algorithm for a class project, and I'm having trouble compiling the class that runs the algorithm. All of the other files compile fine except for this one....
1
by: Nicola | last post by:
Consider the following implementation of a graph, whose nodes must be of type Node or of a subclass of Node: class Node { public: Node(Data* d) { adjList = new vector<Node*>; data = d; }...
1
by: Eric D. Nielsen | last post by:
I'm in the process of implementing a "monitor this" type feature on a web-application. When something changes on the monitored item an email to the subscriber is generated. I'd like to do this...
17
by: Student | last post by:
Hi All, I have an assignment for my Programming language project to create a compiler that takes a C++ file as input and translate it into the C file. Here I have to take care of inheritance and...
0
by: Limpor | last post by:
Hi, I’m working on a solitaire game as my course assignment, and I am having trouble to dealing with Stock class, which consists of an upturned top card plus deck. The code for Stock class: import...
0
by: south622 | last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past...
11
by: lenygold via DBMonster.com | last post by:
I am tryieng to convert our time consuming recursive queries too very efficient queries based on nested set model. The only problem is to convert an adjacency list model into a nested set model,...
3
by: Kinokunya | last post by:
Hi guys, My group and I will be working on our final year project, the scope to do a program/web-based application similar areas of functionalities like the PyLint and PyChecker; a Python syntax...
2
by: msingh00 | last post by:
INPUT = no of vertices. RPRESENTATION -- Adjacency list. DEGREE of each of the vertices = generate randomly within the function. Randomly decide (within the function) which of the vertices are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.