I'm trying to model a simple Graph structure as an UML Class Diagram and Object Diagram. A Graph can have multiple Nodes and knows it's starting Node. Every Node has a predecessor and a successor Node.
This is my Class diagram:enter image description here
My first question is, do I need to add an edge from the Node class to itself, because a Node can have two Nodes? Or is it enough show the relation with the attributes (predecessor and successor)?
Now I want to create an Object diagram with 3 nodes. I'm not sure how to model the relations between these Objects. This is my solution:enter image description here
Is it valid to write predecessor = Null
to show that this particular node has no predecessor?
Is it valid to reference objects by its name, for example when writing successor = midNode
?
Do I need to add the arrows to show the relations or would it be enough to just reference them by for example successor = midNode
?
-
who is the diagram for? for most real world uses the object diagram is already probably too much imhojk.– jk.03/17/2021 10:37:24Commented Mar 17, 2021 at 10:37
-
"do I need to add a from the Node class to itself" Need to add a what? I think there's a word missing here.candied_orange– candied_orange03/17/2021 11:38:46Commented Mar 17, 2021 at 11:38
-
An alternative option could be to represent edges and vertices separately rather than representing edges as references/pointers between vertices. A common way to represent edges on a graph is using an Adjacency table (for example, using a Dictionary/Map structure with an adjacency entry for each vertex).Ben Cottrell– Ben Cottrell03/17/2021 16:16:14Commented Mar 17, 2021 at 16:16
-
@jk. its for an examJonas– Jonas03/17/2021 19:33:37Commented Mar 17, 2021 at 19:33
-
for an exam you are going to have to do whatever your examiner expects, personally i think x=null is fine but if your course has taught something else then you are better to go with whatever that isjk.– jk.03/18/2021 08:09:52Commented Mar 18, 2021 at 8:09
2 Answers 2
This appears to be a Doubly Linked List. Knowing that makes it easier to find good UML examples.
My first question is, do I need to add an edge from the Node class to itself, because a Node can have two Nodes? Or is it enough show the relation with the attributes (predecessor and successor)?
These class diagrams show how to add that edge:
softwareideas.net - Linked Lists
research.cs.queensu.ca - ELEC 372 - Engineering Software Structures - Assignment 1 solution
Is it valid to write predecessor = Null to show that this particular node has no predecessor?
It's unusual. It's more typical to draw an edge leading to ground like in the object diagram below.
Is it valid to reference objects by its name, for example when writing successor = midNode?
It's unusual. Just calling a field "name" doesn't make it a unique identifier. It's more typical to draw an edge to the linked object like in the object diagram below.
uva-cs.github.io - PDR: Laboratory 2: Linked Lists
Do I need to add the arrows to show the relations or would it be enough to just reference them by for example successor = midNode?
The arrow heads show you what knows about what. Leaving them off an edge implies that that knowledge goes both ways. Some people get sloppy about that when diamonds are involved but personally I want to see arrow heads unless it's a real bi-directional relationship.
Every diagram is free to develop it's own symbols but these are the typical UML relationship symbols:
Class diagram
In principle you should either show the association or the attributes. Representing both together is ambiguous, even thought most of the readers resolve the ambiguity very well. In your case I'd recommend the graphical notation:
Your diagram with the attribute notation suggests that each node has only one successor and one predecessor. This looks more like a linked list than a real graph. If you want to use a different multiplicity, you should note it explicitly, e.g.:
successor: Node[*]
If you show it as association, the absence of multiplicity means an unspecified multiplicity. It's easy to specify it, by adding a *
on each end of your association (many-to -many).
Object diagram
The object diagram illustrates very well the representation of a specific graph. It can be practical when discussing a concrete case.
The notation should be consistent with the class diagram. So keep attributes if you used attributes, or show links if you used association. Don't add arrows: arrows are reserved for navigability, and if there is a navigability, you'd best show it in the class diagram as well (see consistency principle above)
Finally, the notation predecessor=null
will be understood by everybody (UML specifications, section 8.2.4, defines null
litteral). If the multiplicity is different from 1, you can show the attribute value as a collection, e.g. Set { a,b,c }
(see UML specs section 7.5.3.2 for the type of collection, and see OCL specs section 7.5.11 for the litteral notation). The absence of predecessors/successors is then shown as an empty collection. The graphical notation is more intuitive: you just don't draw the missing link.