![[Pasted image 20250314141620.png]] A graph is a general term for a structure consisting of nodes connected by zero-to-many edges. They are (at least in my opinion, and possibly factually verifiable) the most versatile means of organizing information. [[Everything can be represented in a graph]] - i.e. graphs can recreate _any_ other form of data structure (e.g. [[Hierarchy]] or [[Relational Databases]]). This versatility is the graph's killer feature, but also perhaps its Achilles heel. There's no wrong way to do a graph, and because they are so flexible and general it is hard to write efficient tools that operate in such a flexible structure. Many things you **could** do in a graph are better optimized by things like [[Relational Databases]]. Graphs are stored in [[Graph Database]]s. [[Graph Theory]] is the branch of mathematics having to do with the study of graphs. **Misc facts:** - Graphs can be directed (a directed graph is a "digraph"), or undirected. - If nodes and edges have properties, then it's commonly called a [[Property Graph]] - Directed graphs may be cyclic (you can draw a loop using edges), or [[DAG|acyclic]] (where no loops exist). - [[Resource Description Framework|RDF]] is essentially a graph structure - node, node, edge. - Allowed nodes & edges in a graph are its [[Ontology]]. - More complex relations are possible through the use of hyperedges & edge nodes - **Hyperedge** - in "[[Hypergraph]]s", an edge connecting >2 nodes - Edge Node - an edge that acts like a node, can be connected to by other edges and/or have its own properties - [[Wikidata]] uses edge nodes - You can represent a graph using an [[Adjacency Matrix]] # Constructing a Graph You can create an arbitrary graph structure using [[JSON]]. ```json { nodes: [ { id: "A",f label: "Aaron", // any other arbitrary data type: "human", age: 36, }, { id: "B", label: "Loki", type: "cat" // nodes may or may not be forced to have the same set of properties } ], edges: [ { from: "A", to: "B", label: "Owns" }], } ``` Doing this, though, introduces interpretation errors. This actually makes for a perfect lead-in to the concept of [[Ontology]] engineering and [[Resource Description Framework]]. In short, those concepts remove ambiguity by pointing to globally unique [[URI]]s that define properties in unambiguous terms. It also introduces conventions like prefixing known, important properties with "@", like in the example below. ```JSON { "@context": { "foaf": "http://xmlns.com/foaf/spec/" }, "@graph": [ { "@id": "https://example.org/my-profile", // Your unique identifier "@type": "foaf:Person", "foaf:name": "Your Name", "foaf:mbox": "mailto:[email protected]" // Example FOAF property } ] } ``` Also demonstrated in that example is the aliasing of known ontological source prefixes. There `foaf` used in the graph is an alias for the text referred to in by the `foaf` key in the @context. ## Other graph constructions You don't have to use [[JSON]]. You could construct a graph using [[CSV]] files, like how [[Archi]] exports work. There is also [[Turtle]] and, more generally, the [[Resource Description Framework]]. ### CSV File Example A full & arbitrarily complex [[Property Graph]] in 3 csv files. **nodes.csv** ```csv id, arbitrary_other_fields, ... a, Aaron, ... b, Bob, ... ``` **edges.csv** ```csv id, from, to, arbitrary_other_fields, ... ab, a, b, Aaron knows bob, ... ``` **props.csv** ```csv id, key, val a, age, 37 a, gender, male b, age, 39 b, gender, male ab, since, 2022 ``` # Graphs in the Wild - Graphs are behind so-called "friend of a friend" recommendation algorithms - The [[JavaScript]] visualization library "GoJS" and VisJS use graph structures for their visualizations - The productivity product **Asana** credits their graph-based approach for how to scale work-related - see [[Asana Work Graph]] - A crazy amount of [[Diagram Types (index)|diagrams]] are just graphs - e.g. - [[Graph Visual Representation|Petri Net]], [[IDEF0]], [[IDEF3]], [[Flow Charts]], [[State Diagrams|State Machine Diagrams]] and essentially every form of [[SysML]], [[UML]], and [[Archimate]] diagram[^1] **** # More ## Source - Self/experience - Bit of Gemini, for the RDF syntax - https://guides.sharafat.co.uk/documents/Graph_Theory_for_Dummies.pdf ## Related [^1]: If you allow for containment being represented using a graph structure, that is. [[Archi]] and Archimate natively support this.