Jump to content

Graph problem (Data Struct & Algorithms)

smjpl
Go to solution Solved by smjpl,

lol. I hate programming :lol:.

 

It should have been

testGraph.addVertex('a');

instead of

testGraph.addVertex(a);

Now it works. :)

I am going through the book "Data Structures an Algorithms" by Robert Lafore and I am having an issue with one piece of code. All the code apart from main was taken from his book. Can someone tell me what displayVertex() is meant to do? Look for // THIS PART HERE!!!!!!!!!!!!!!!!!!*************

 

I would assume it prints out the vertex held in the vertexList array position but nothing actually happens when I use it. It runs fine but displays nothing to the console.

 

PS. I suck at programming so go easy on me. :)

class Graph{        private int MAX_VERTS = 20;    private Vertex vertexList[]; // array of vertices    private int adjMat[][]; // adjacency matrix    private int nVerts; // current number of vertices    // -------------------------------------------------------------       public Graph() // constructor    {    vertexList = new Vertex[MAX_VERTS];    // adjacency matrix    adjMat = new int[MAX_VERTS][MAX_VERTS];    nVerts = 0;    for(int j=0; j<MAX_VERTS; j++) // set adjacency    for(int k=0; k<MAX_VERTS; k++) // matrix to 0    adjMat[j][k] = 0;    } // end constructor       // -------------------------------------------------------------    public void addVertex(char lab) // argument is label    {    vertexList[nVerts++] = new Vertex(lab);    }    // -------------------------------------------------------------    public void addEdge(int start, int end)    {    adjMat[start][end] = 1;    adjMat[end][start] = 1;    }    // -------------------------------------------------------------        // THIS PART HERE!!!!!!!!!!!!!!!!!!*************    public void displayVertex(int v)    {    System.out.print(vertexList[v].label);    }// -------------------------------------------------------------} // end class Graphclass Vertex{    public char label; // label (e.g. ‘A’)    public boolean wasVisited;    public Vertex(char lab) // constructor    {        label = lab;        wasVisited = false;    }} // end class Vertexclass graphApp{        public static char a;    public static char b;    public static void main(String [] arg){                Graph testGraph = new Graph();                               testGraph.addVertex(a);        testGraph.addVertex(b);                testGraph.addEdge(a, b);               testGraph.displayVertex(0);               System.out.println("I don't know what is happening!!!!!!");   }}       

Rig: i7 2600K @ 4.2GHz, Larkooler Watercooling System, MSI Z68a-gd80-G3, 8GB G.Skill Sniper 1600MHz CL9, Gigabyte GTX 670 Windforce 3x 2GB OC, Samsung 840 250GB, 1TB WD Caviar Blue, Auzentech X-FI Forte 7.1, XFX PRO650W, Silverstone RV02 Monitors: Asus PB278Q, LG W2243S-PF (Gaming / overclocked to 74Hz) Peripherals: Logitech G9x Laser, QPad MK-50, AudioTechnica ATH AD700

Link to comment
Share on other sites

Link to post
Share on other sites

Eclipse prints out a square box for me, which I assume means it's an unreadable character... weird.

 

First, you're not passing anything to with your testGraph.addVertex(a) method call. You never gave char a or char b an initial value.

 

Secondly, your testGraph.addEdge(a, b ); would pass char a and b as their ascii values to the method, because the method parameters are ints. See this table: http://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html

 

But displayVertex is only called once, and carries the arg "0". So each time you run the program, it'll only print the first element of the array. 

 

CPU Intel Core i7-4790K Motherboard ASUS Saberthooth Z97 Mark1 RAM Corsair Dominator Platinum 16GB @ 2133MHZ GPU 2 X MSI GeForce GTX GTX 980TI GAMING 6G SLI Case Phantex Enthoo Evolv ATX Storage Samsung 840 Pro Series 256GB / Western Digital Caviar Black 2TB PSU Corsair RM1000i Display Asus 4K PB287Q Cooling Noctua NF-A14 FLX & NF-F12 PWM /Thermaltake Water 3.0 Ultimate 360mm Keyboard Corsair K70 Mouse ROCCAT Kone XTD Sound Card Asus Xonar STX

Link to comment
Share on other sites

Link to post
Share on other sites

a and b aren't initialized to anything. Could be 0, could be anything.

 

Also, I'm not sure that you want to be indexing adjMat with those char values in the call testGraph.addEdge(a, b). I would think you want testGraph.addEdge(0, 1) in this case.

main(i){for(;i<101;i++)printf("Fizz\n\0Fizzz\bBuzz\n\0%d\n"+(!(i%5)^!!(i%3)*3)*6,i);}

Link to comment
Share on other sites

Link to post
Share on other sites

before diving into data structures you should have a basic knowledge of the language you're going to use, and programming in general

i mean... my point is that you're trying to work on [complex] data structures, but you're not familiar with single simple variables yet

maybe you should do a step back

Link to comment
Share on other sites

Link to post
Share on other sites

a and b aren't initialized to anything. Could be 0, could be anything.

 

Also, I'm not sure that you want to be indexing adjMat with those char values in the call testGraph.addEdge(a, b). I would think you want testGraph.addEdge(0, 1) in this case.

 

Yep you were right about "testGraph.addEdge(0, 1)" My bad. I didn't read it correctly. I am still confused about the vertexList though. I made it public in the class Graph so I could work with it directly from main. When I look at what is actually contained in the array vertexList[], every position of that array results "Vertex@4fee225".

main(){         System.out.println(testGraph.vertexList[2]);       }

console: Vertex@4fee225

 

 

but when I execute the code that is meant to be there

System.out.println(testGraph.vertexList[2].label);

it returns nothing.

 

I am just a bit confused.

Rig: i7 2600K @ 4.2GHz, Larkooler Watercooling System, MSI Z68a-gd80-G3, 8GB G.Skill Sniper 1600MHz CL9, Gigabyte GTX 670 Windforce 3x 2GB OC, Samsung 840 250GB, 1TB WD Caviar Blue, Auzentech X-FI Forte 7.1, XFX PRO650W, Silverstone RV02 Monitors: Asus PB278Q, LG W2243S-PF (Gaming / overclocked to 74Hz) Peripherals: Logitech G9x Laser, QPad MK-50, AudioTechnica ATH AD700

Link to comment
Share on other sites

Link to post
Share on other sites

before diving into data structures you should have a basic knowledge of the language you're going to use, and programming in general

i mean... my point is that you're trying to work on [complex] data structures, but you're not familiar with single simple variables yet

maybe you should do a step back

 

lol. I wish I could. I am doing a masters course in applied computing. It is a conversion course to computers from other scientific disciplines. I studied mech engineering before this. We basically had one semester of java to get the basics and now the second semester is data structures and algorithms. I agree that I am jumping the gun but I have no choice. Thanks for the vote of confidence though :P

Rig: i7 2600K @ 4.2GHz, Larkooler Watercooling System, MSI Z68a-gd80-G3, 8GB G.Skill Sniper 1600MHz CL9, Gigabyte GTX 670 Windforce 3x 2GB OC, Samsung 840 250GB, 1TB WD Caviar Blue, Auzentech X-FI Forte 7.1, XFX PRO650W, Silverstone RV02 Monitors: Asus PB278Q, LG W2243S-PF (Gaming / overclocked to 74Hz) Peripherals: Logitech G9x Laser, QPad MK-50, AudioTechnica ATH AD700

Link to comment
Share on other sites

Link to post
Share on other sites

lol. I hate programming :lol:.

 

It should have been

testGraph.addVertex('a');

instead of

testGraph.addVertex(a);

Now it works. :)

Rig: i7 2600K @ 4.2GHz, Larkooler Watercooling System, MSI Z68a-gd80-G3, 8GB G.Skill Sniper 1600MHz CL9, Gigabyte GTX 670 Windforce 3x 2GB OC, Samsung 840 250GB, 1TB WD Caviar Blue, Auzentech X-FI Forte 7.1, XFX PRO650W, Silverstone RV02 Monitors: Asus PB278Q, LG W2243S-PF (Gaming / overclocked to 74Hz) Peripherals: Logitech G9x Laser, QPad MK-50, AudioTechnica ATH AD700

Link to comment
Share on other sites

Link to post
Share on other sites

I studied mech engineering before this.

lol. I hate programming :lol:.

now i see

you're from... the other side of engineering

the bad side

how dare you enter this subforum

leave! now!

Link to comment
Share on other sites

Link to post
Share on other sites

now i see

you're from... the other side of engineering

the bad side

how dare you enter this subforum

leave! now!

 

lol, We all like to build things. Can't we all live in peace and harmony? :P

Rig: i7 2600K @ 4.2GHz, Larkooler Watercooling System, MSI Z68a-gd80-G3, 8GB G.Skill Sniper 1600MHz CL9, Gigabyte GTX 670 Windforce 3x 2GB OC, Samsung 840 250GB, 1TB WD Caviar Blue, Auzentech X-FI Forte 7.1, XFX PRO650W, Silverstone RV02 Monitors: Asus PB278Q, LG W2243S-PF (Gaming / overclocked to 74Hz) Peripherals: Logitech G9x Laser, QPad MK-50, AudioTechnica ATH AD700

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×