Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding weight to a directed graph.  RSS feed

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
I have been trying for hours to add weight to my graph only to really mess things up (luckily I back things up). Any advice to add weights to my graph would be amazing. This program prints out an adjacency list  I want it to print out the adjacency list with the weight next to the node. I would post what I have worked on but I deleted it and I am starting back from the start.





and my main is
 
Saloon Keeper
Posts: 8618
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your names and data structures are very confusing to me. Why is an array of vertices called arrayOfLists, and not simply vertices? Why does a node have a vertex index, and a next node? Why does a vertex have a node, and why is it not the same thing as a node? Why are you using arrays and not data structures that grow dynamically and can easily index their contents, such as a Map?

If I made a graph data structure, I would let a node/vertex keep track of their name/key, neighbors and maybe an associated value (which in this case you can use for weights). A graph keeps track of all the nodes by mapping them against their keys. You can use generic type parameters for the node's key and value types.
 
Ranch Hand
Posts: 4702
9
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading a book right now that tells you exactly how to do it. unfortunately I don't have it with me right now. you might be able to access it at algs4.cs.princeton.edu
 
Randall Twede
Ranch Hand
Posts: 4702
9
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
they do discuss it on the book site.
http://algs4.cs.princeton.edu/44sp/
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!