2

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?

6
  • who is the diagram for? for most real world uses the object diagram is already probably too much imho
    – jk.
    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. 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). Mar 17, 2021 at 16:16
  • @jk. its for an exam
    – Jonas
    Mar 17, 2021 at 19:33
  • @candied_orange yeah thanks, fixed it
    – Jonas
    Mar 17, 2021 at 19:33

2 Answers 2

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:

enter image description here

softwareideas.net - Linked Lists

enter image description here

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.

enter image description here

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:

enter image description here

ionos.com - Creating class diagrams with UML

2

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:

enter image description here

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.