Nodes and Adjacency Matrix Computer Programming C++

Nodes and Adjacency Matrix Computer Programming C++

Page 1 of 1

11 Replies - 7966 Views - Last Post: 07 December 2010 - 02:32 AM Rate Topic: -----

#1 tKc  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 38
  • Joined: 25-October 10

Nodes and Adjacency Matrix Computer Programming C++

Posted 03 December 2010 - 05:50 PM

Hokay so, i gotta make a program that reads an adjacency matrix from a data file and then outputs which nodes are adjacent to the others. In the data file there are 0's and 1's, 0 means that they are not connected and a 1 means that they are. I got some of my code that allows me to remove and insert necessary nodes in the correct spot however im an stumped on my int main(){ and have no clue on what direction i should go or where i should start any comments would be much appreciated.
#include <iostream>
#include <fstream>
using namespace std;
    
        typedef struct node{
            int data;
            node* link;
        } node;
    
      node* begin(node* linkHead){
          return (node*)linkHead -> link;
          }
          
    node* end(node* linkHead){
          node* x = linkHead;
          while(x->link != NULL){
                       x = x->link;
                       }
                       return x;
          }
                       
    bool empty(node* linkHead){
         if(linkHead == NULL){
                     return(true);
                     }
    }
                     
    node* insert ( node* x , int value){
          node newnode;
          newnode.data = value;
          newnode.link = x -> link;
          x -> link = &newnode;
          return x;
          }
          
          
          node* remove (node* linkHead, int value){
                 if( linkHead = NULL)
                     return NULL;
           node*x = linkHead;
           node*prev = x->link;
           x=x->link;
           while(x->link != NULL){
                        if(x->data == value){
                                  prev ->link = x->link;
                                  delete x;
                                  x = prev;
                                  prev = x->link;
                                  x = x->link;
                        }
           }       
}
          
          

         
int main(){

    ifstream fin("data1.txt");
    ofstream fout("adjacencylist.txt");


return 0;
}


(My data file)
0 1 1 0 1
1 0 1 1 1
1 1 0 0 0
0 1 0 0 1
1 1 0 1 0

Is This A Good Question/Topic? -1
  • +

Replies To: Nodes and Adjacency Matrix Computer Programming C++

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Nodes and Adjacency Matrix Computer Programming C++

Posted 03 December 2010 - 07:43 PM

Very impressive. Don't know where you lifted that from, but node and linked list code has absolutely no relevance to this assignment. So you're back at square 0.

You should probably start with reading about what an adjacency matrix is. Google can probably help you there. Also, I imagine, your textbook.

This post has been edited by r.stiltskin: 03 December 2010 - 07:45 PM

Was This Post Helpful? 0
  • +
  • -

#3 tKc  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 38
  • Joined: 25-October 10

Re: Nodes and Adjacency Matrix Computer Programming C++

Posted 03 December 2010 - 09:13 PM

yeah, really none of them do do anything (other than our teacher telling is it was good practice after we have completed it) except for insert and remove which allows us to make and insert a temporary node then delete it after. the trouble im having is how do we implement this code into our main. teacher helped us with the insert and remove, not the rest tho
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Nodes and Adjacency Matrix Computer Programming C++

Posted 03 December 2010 - 10:40 PM

You're not listening. NONE of that, not the insert, not the remove, and not even the node, has anything to do with an adjacency matrix. You could use it for adjacency lists, but not for a matrix. Makes no sense. An adjacency matrix is just a 2-dimensional array, and the "nodes" of the graph are just the labels of the rows and columns of the array. There's no need for node structures or node* pointers.

This post has been edited by r.stiltskin: 03 December 2010 - 10:42 PM

Was This Post Helpful? 0
  • +
  • -

#5 tKc  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 38
  • Joined: 25-October 10

Re: Nodes and Adjacency Matrix Computer Programming C++

Posted 03 December 2010 - 10:43 PM

also i know what an adjacency matrix does. i did create this one after all
1 2 3 4 5
----------
1 |0 1 1 0 1
2 |1 0 1 1 1
3 |1 1 0 0 0
4 |0 1 0 0 1
5 |1 1 0 1 0
----------
where there is a 0 they are not connected (1-1 2-2 3-3... are 0's because they cannot be connected to themselves), 1's mean they are connected
Was This Post Helpful? 0
  • +
  • -

#6 tKc  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 38
  • Joined: 25-October 10

Re: Nodes and Adjacency Matrix Computer Programming C++

Posted 03 December 2010 - 10:52 PM

View Postr.stiltskin, on 03 December 2010 - 09:40 PM, said:

You're not listening. NONE of that, not the insert, not the remove, and not even the node, has anything to do with an adjacency matrix. You could use it for adjacency lists, but not for a matrix. Makes no sense. An adjacency matrix is just a 2-dimensional array, and the "nodes" of the graph are just the labels of the rows and columns of the array. There's no need for node structures or node* pointers.

kk, basically what i ment just never knew the diff between list and matrix. So i create the adjacency matrix my self by hand and put the values into a data file(done). program is to read the file and output the adjacency list into a new data file.
Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Nodes and Adjacency Matrix Computer Programming C++

Posted 03 December 2010 - 11:51 PM

Five adjacency lists -- one for each vertex of the graph.

Ok, so what's the problem? Now you can use your nodes to represent the vertices of the graph. Use your node's 'data' member as its "ID" (numbers 0-4 or 1-5 for example),, You can represent the graph as an array of 5 vertices (nodes) and use each vertex's "link" pointer to point to the first vertex in its adjacency list. Just read the data from the file row by row and add nodes to the lists when you find a 1.
Was This Post Helpful? 0
  • +
  • -

#8 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1574
  • View blog
  • Posts: 2,996
  • Joined: 30-May 10

Re: Nodes and Adjacency Matrix Computer Programming C++

Posted 04 December 2010 - 02:09 AM

http://www.daniweb.c...read329201.html

http://www.catb.org/...ions.html#forum

Honestly, it's getting harder and harder every day to even bother reading a forum and thinking about answering a question when you know full well that it could be a COMPLETE WASTE OF ************* TIME when it has already been answered somewhere else.
Was This Post Helpful? 1
  • +
  • -

#9 tKc  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 38
  • Joined: 25-October 10

Re: Nodes and Adjacency Matrix Computer Programming C++

Posted 05 December 2010 - 02:30 AM

okay so this is what i gots so far to try and create the link list
int main(){
    int x, n;
    ifstream fin("data1.txt");
    ofstream fout("adjacencylist.txt");
    cin >> n;
    
node heads[n];

for (int i=0; i<n; i++){
heads[i].data=i+1;
heads[i].link=NULL;
for (int j=0; j<n; j++)
{
fin>>x;
if (x==1) insert(heads[i], j+1);
}
}
    
system("PAUSE");
return 0;
}


i am having trouble on line 15, my compiler is saying

cannot convert 'node' to node*' for argument '1' to 'node* insert(node*, int)'

also i changed my insert to
node* insert ( node* x , int value){
if (NULL == x) return NULL;

node* newnode = new node;
newnode->data = value;
newnode->link = x -> link;
x -> link = newnode;
return x;
}


Was This Post Helpful? 0
  • +
  • -

#10 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Nodes and Adjacency Matrix Computer Programming C++

Posted 05 December 2010 - 03:03 AM

So what part of this don't you understand?

View PosttKc, on 05 December 2010 - 06:30 PM, said:

cannot convert 'node' to node*' for argument '1' to 'node* insert(node*, int)'



Read it. Look at your code.
You have a function that expects to receive node * (a pointer to a node).
You are passing it a node (not a pointer)

That's it. That is the error.
That's what the error message says and that is what you are doing in your code.
Look at your code, do you disagree?

So what should you do, do you think?

EDIT
@ tKc
Thanks for the down vote. That certainly shows you are one of life's winners (or maybe not).
So if this was not helpful to you then post something that explains why it was not helpful.
Clicking the negative button alone is not enough. You need to say why you voted that way.
What was wrong with what I posted?
If it was just that it hurt your feelings then say so.
If there is something wrong with it then you need to say so.

This post has been edited by janotte: 05 December 2010 - 04:31 AM

Was This Post Helpful? 0
  • +
  • -

#11 tKc  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 38
  • Joined: 25-October 10

Re: Nodes and Adjacency Matrix Computer Programming C++

Posted 05 December 2010 - 06:00 PM

should be pretty obvious but its straight up rude.

Quote

read it. look at your code.
That's it. That is the error

There's no need for that and your post would have been just as effective if those comments were not added. I know that's the error.
I have read it (and yes i believe i have corrected my code) but ur pretty much taken me for a straight up retard.
Was This Post Helpful? 0
  • +
  • -

#12 tKc  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 38
  • Joined: 25-October 10

Re: Nodes and Adjacency Matrix Computer Programming C++

Posted 07 December 2010 - 02:32 AM

well im done son
here's what i got for my main
int main(){
    int x, n, y;
    ifstream fin("data.txt");
    ofstream fout("adjacencylist.txt");
    fin >> n;
    
    node heads[n];

    for (int i=0; i<n; i++){
         heads[i].data=i+1;
         heads[i].link=NULL;
         for (int j=0; j<n; j++){
             fin>>x;
             if (x==1) insert(&heads[i], j+1);
         }
     }

     for (int k = 0; k < n; k ++){
         node* y = &heads[k];
         while ( y != NULL){
                     fout << y->data  << " ";
                     y = y -> link;
         }        
                                            fout << endl;    
                                            
     }   
system("PAUSE");
return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1