The Big Programming Thread - Page 797
Forum Index > General Forum |
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. | ||
Blitzkrieg0
United States13132 Posts
| ||
![]()
NovemberstOrm
Canada16217 Posts
xcode 7.1 swift 2 i believe I have one Textfield(playerAnswerTextView) on ViewController1 and a UI label(userAnswerTextBox) on ViewController2, I want the textfield to display its text to the ui label, segues are setup, i have a button setup to go to the next view controller. I've tried to get it work but can't seem to for some reason :/ Help is appreciated. | ||
Nesserev
Belgium2760 Posts
| ||
Prillan
Sweden350 Posts
On November 10 2016 12:52 Manit0u wrote: ![]() This is great! | ||
Djagulingu
Germany3605 Posts
| ||
Deleted User 3420
24492 Posts
I am thinking that my graph class will have a map members:
where the integer represents the weight of the edge So My map would store each node, as well as a map of nodes it connects to and the weight of the edge between them. Is this a good solution? Are there alternatives any of you would prefer? Also in lecture, the professor kept mentioning "nodes" and "vertexes". Though for the life of me I didn't understand the difference and he didn't (couldn't?) really explain the difference. Is it that in a typical representation, the vertex is simply a representation of the spot on the graph, and the node would be an object that contains the vertex and any other associated data? But in that case if there was no other associated data it would make sense to just represent it with nodes instead of vertexes, right? Okay, thanks! | ||
Acrofales
Spain17831 Posts
Your graph is simply a list of nodes Every node has a list of edges in the form: Map<Node, Integer> weightededges However, this doesn't work for some common graph problems (such as max-flow-min-cut) where you want a list of edges at the top level. It also means that if your graph is not directed, you are storing each edge twice, which is slightly redundant (but usually not a problem). There is no difference between nodes and vertices. Node is used in networks, and vertices in mathematics. Both mean the same thing. If you need your edges at the top level for the functioning of your algorithm, generally a good solution is to have an edge data structure with fields: Node start Node end int weight And have a List<Edge> at the top level. However, in this case you will have to do extra work to ensure your graph is sane, and you don't have multiple edges between the same 2 nodes. I'm not a big fan of your solution of Map<Node, Map<Node, Ingeger>>: it takes care of keeping the graph sane, but it is not as easy to do operations on (such as order all edges by weight): it is a compromise between the node-based solution and the edge-based solution, without the advantages of either. | ||
Deleted User 3420
24492 Posts
that all makes sense So, if my graph got really really huge, using lists of edges would be bad, right? Wouldn't sets be better? edit: your last edit touches on my project requirements a lot. im gonna have to think about that. Let's say I was going to have to implement djiksta's algorithm What structure would you recommend then? | ||
RoomOfMush
1296 Posts
On November 14 2016 02:22 travis wrote: Thanks acrofales that all makes sense So, if my graph got really really huge, using lists of edges would be bad, right? Wouldn't sets be better? Why would sets be somehow better than lists? An ArrayList is pretty much the fastest data structure for append operations and iterations. Only when it comes to random access insertions and deletions is the ArrayList somewhat slow (but still pretty fast). On November 14 2016 02:22 travis wrote:Let's say I was going to have to implement djiksta's algorithm What structure would you recommend then? I would recommend having a Node class and an Edge class. An Edge connects two Node's. Each Node has a List of Edge's to other Node's which are connected. You can also have a Graph class which has a List of all Node's. The List of Edge's is useful because it can be sorted by weight which makes it easy to pick the "cheapest" path first. | ||
Deleted User 3420
24492 Posts
yeah basically exactly what you are talking about. there's so many ways to implement this lol | ||
Acrofales
Spain17831 Posts
E: had this reply open without posting. Having both nodes and edges is a "complete" representation of a graph, but you'll find it's really redundant most of the time. For starters, at no point in Dijkstra will you be interested in knowing about all edges, so why bother representing them explicitly. Having a list of edges in each node is effectively the same as having a list of nodes in each node. It's just encapsulated in an imho over-engineered OOP manner. The reason you want a Set (in my example, and your initial idea, the keys in the map) is so you don't have to check uniqueness of each edge. | ||
Deleted User 3420
24492 Posts
![]() I'll probably be back in a few days with more questions lol | ||
Mr. Wiggles
Canada5894 Posts
On November 14 2016 01:05 travis wrote: I need to write a weighted graph in java. Any opinion on the best way to implement this? I am thinking that my graph class will have a map members:
where the integer represents the weight of the edge So My map would store each node, as well as a map of nodes it connects to and the weight of the edge between them. Is this a good solution? Are there alternatives any of you would prefer? Also in lecture, the professor kept mentioning "nodes" and "vertexes". Though for the life of me I didn't understand the difference and he didn't (couldn't?) really explain the difference. Is it that in a typical representation, the vertex is simply a representation of the spot on the graph, and the node would be an object that contains the vertex and any other associated data? But in that case if there was no other associated data it would make sense to just represent it with nodes instead of vertexes, right? Okay, thanks! I don't believe there's any technical difference between nodes and vertices. They're basically synonyms when talking about graphs. For example, I usually refer to vertices, but others use the word nodes. In addition to what's been suggested, another possible graph representation is the adjacency-matrix: https://en.m.wikipedia.org/wiki/Adjacency_matrix This is generally useful for doing random lookups of if two vertices are connected. On November 14 2016 03:28 travis wrote: Okay so I basically went with what I think you were saying, I am going to use an arraylist of edges. yeah basically exactly what you are talking about. there's so many ways to implement this lol Instead of repeated sorting to find the minimum edge distance, you can also look at using a priority-queue implementation. https://en.m.wikipedia.org/wiki/Priority_queue https://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html This will generally be faster than re-sorting the list at every step of Dijkstra's algorithm. It's also a very useful data structure to know in general, when you want to keep a sorted queue of something where you're popping and pushing members. | ||
Deleted User 3420
24492 Posts
| ||
phar
United States1080 Posts
| ||
RoomOfMush
1296 Posts
On November 14 2016 04:01 travis wrote: In java, is there a time you need to use "this" to specify what member you are referring to *other* than when you have duplicate field names? (like, are passed parameter "key" and have a class member "key" so in your constructor you do this.key = key ... which is obviously bad anyways) There is a few obvious situations like returning "this" or passing "this" as an argument. Other than that it might happen when dealing with non-static inner classes that the inner class and the outer class have a method with the same name (or perhaps a member with the same name) and you need to refer to both of them, for example: public class Test { If you would leave the "Test.this." out you would get an infinite recursion. | ||
Deleted User 3420
24492 Posts
I have an arraylist filled with objects each object has a map I find one of the objects I do a shallow copy I then update the map of the shallow copy I then do a indexOf(shallow copy) on the arraylist will it find the index of the original object? It will, right? edit: I don't even need to put the shallow copy in the arraylist do I? by updating the map I also update the map of the original object? Is that correct? edit: yes. yes that is correct. | ||
AKnopf
Germany259 Posts
About indexOf(), it depends on how you implemented the equals()-Method in your custom class. The ArrayList just goes through its elements until it finds the first element that returns true for equals(). If you have not defined your own version of equals() and you are not inherting from a class that implements equals(), then you use the default implementation, which is for complexe types a reference check. This means that only the exact same object is equal. The copy is not equal. Further reading:
Most importantly: Always also implement hashCode() when you implement equals() in a way, that two object that are equal also produce the same hash code. Otherwise you will land in programmers hell when you want to use your objects in a HashMap. Cheers! Update: Fixed sentence about reference vs value. Thanks, Manit0u! | ||
Manit0u
Poland17185 Posts
On November 14 2016 05:13 AKnopf wrote: Primitive types are referenced by value while complex types are referenced by value. You might want to revisit this sentence ![]() | ||
Nesserev
Belgium2760 Posts
| ||
| ||