r/computerscience • • Oct 06 '26

Discussion A linkedlist is a skewed directed graph

Can I say the above? I think of this after studying trees and graph. Singly linked list in particular.

23 Upvotes

36 comments sorted by

61

u/Flashy-Guava9952 Oct 06 '26

If I recall correctly, a linked list is a special case of a tree, which is special case of a graph.

-11

u/ResponsibilityBig343 Oct 06 '26

Yeah but we can't say the same thing for doubly linkedlist or circular doubly cause there every adjacent node pair forms a cycle.

47

u/Big-Rub9545 Oct 06 '26

Those aren’t trees, but they’re certainly graphs.

3

u/Flashy-Guava9952 Oct 06 '26

You could have a tree's children link back to their parents, allowing for doubly linked lists.

1

u/ResponsibilityBig343 Oct 06 '26

Thats not how trees work i think. Because this would break hierarchy.

4

u/FastSlow7201 Oct 07 '26

Trees, singly linked list = directed graphs where all of the direction is uniform from one node to the next

doubly linked list = undirected graph

In a traditional graph every node could connect to every other node, which is a fully connected graph. A tree has the limitation of two connections (previous and next).

2

u/Flashy-Guava9952 Oct 06 '26

It works just fine. It's just an extra piece of information you are free to ignore.

1

u/__chicolismo__ Oct 06 '26

Good luck walking that tree

5

u/Flashy-Guava9952 Oct 06 '26

You walk it like any other tree, just like a doubly linked list still works as a singly linked list.

-1

u/__chicolismo__ Oct 06 '26

You do know graphs with cycles aren't trees right 

14

u/ilovemacandcheese Oct 06 '26 edited Oct 06 '26

Trees are defined in graph theory as connected, undirected graphs with no cycles. Traversing back and forth along an undirected edge doesn't count as a cycle.

In computer science, we often treat "trees" as directed and rooted, along with being connected and acyclic. A doubly linked list can be thought of as an undirected graph, where traversing from parent to child and back to parent doesn't count as a cycle.

6

u/JanetPistachio Oct 07 '26

Trees are definitionally acyclic with V - 1 edges, which means that a doubly linked list cannot be a tree.

3

u/ccppurcell Oct 06 '26

You're right, but, what is more primitive really, the graph or the directed graph? It's tempting to think of a directed graph as a graph with some extra information. But actually when you create an edge it is inherently ordered. You must write one vertex in the memory before the other. So one way to think of undirected graphs is that they are directed graphs with the special property that whenever uv is an edge, vu is also an edge. In that sense a doubly linked list is an undirected path.

1

u/ResponsibilityBig343 Oct 06 '26

Yeah definitions differ based on directed vs undirected.

20

u/ilovemacandcheese Oct 06 '26

Yep. Every kind of data structure can be described with discrete math and, if you understand that, algorithms and data structures becomes much easier.

5

u/CrackerJackKittyCat Oct 06 '26

Yes, in some sense is a degenerate graph. But I'd consider that a curiosity insight at most. Do not then go on to discuss it in terms of graph theory or edges and vertices and graph algorithms else would be thought of as being overly clever.

They have extremely different use cases.

2

u/FastSlow7201 Oct 07 '26

To compound what you're already looking at, matrices are graphs and vice versa.

https://www.youtube.com/watch?v=PB-1_JTHyEU

3

u/DTux5249 Oct 06 '26

Yeah, lists are a subset of trees, which themselves are a subset of graphs.

3

u/[deleted] Oct 06 '26

Linked lists are unary trees. In graph theory terms, it is a directed tree (therefore it is connected and acyclic) where every vertex has in degree and out degree <= 1.

2

u/alnyland Oct 06 '26

A single point is a cycle

1

u/amarao_san Oct 07 '26

A graph can be represented not as tree, so not every linear graph is a linked list. E.g. a Vector/Array is a well-packed linear graph, but not a linked list.

1

u/Afraid-Locksmith6566 Oct 06 '26

nope it is just a special case of tree structure - a unary tree, which trees are a special case of DAG - which is a special case of Graph

4

u/aka1027 Oct 07 '26

A DAG is a directed acrylic graph. IDK why are you saying “nope.”

1

u/not_a_bot_494 Oct 06 '26

All datastructures can be described by graphs. Ik fact all programs can be described by graphs.

6

u/[deleted] Oct 06 '26

[removed] — view removed comment

1

u/ResponsibilityBig343 Oct 06 '26

I mean in terms of a tree.

3

u/editor_of_the_beast Oct 06 '26

Graphs can represent structures that have many relationships. “Linear” means that those relationships are skewed toward a single line.

1

u/[deleted] Oct 06 '26

[removed] — view removed comment

1

u/Flashy-Guava9952 Oct 07 '26

What happens if you have an unbalanced binary search tree, and keep inserting numbers in order? 1, 2, 3, 4, 5 etc.

-1

u/[deleted] Oct 07 '26

[removed] — view removed comment

1

u/Flashy-Guava9952 Oct 07 '26

8 paragraphs just to dispute can-be-represented-as. smh

0

u/editor_of_the_beast Oct 06 '26

It’s a graph.

1

u/coolmint859 Oct 06 '26

From a tree perspective each node only has one child, which makes it a skewed tree.