Basic Graph ADT
Table of contents
Definitions
Let
is a set of vertices. is a set of edges.
Types of Graphs
- Undirected Graph: edges have no direction
- Directed Graph: edges have direction
- Weighted Graph: edges have weights
- Unweighted Graph: edges have no weights
- Cyclic Graph: contains a cycle
- Acyclic Graph: does not contain a cycle
- Connected Graph: there is a path between every pair of vertices
- Complete Graph: every pair of vertices is connected by an edge
Path
We call a sequence of edges connecting vertices in a graph a walk.
A path is a walk where all connected vertices are unique.
Reachability
Vertex
Connected Graph
A graph is connected if there is a path between every pair of vertices.
Connected Components
A subgraph of
In addition, it must not be reachable to any other vertex outside the subgraph.
Cycle
A cycle is a path that starts and ends at the same vertex.
Simple Graph
A simple graph is an undirected graph with no self-loops or multiple/parallel edges.
Unless stated otherwise, the term graph refers to a simple graph.
Complete Graph
A complete graph is a simple graph where every pair of vertices is connected by a single unique edge.
Number of Edges in a Complete Graph
A complete graph with
undirected edges.
Comparison to Trees
- Graphs are more general than trees
- All trees are graphs
- Trees are connected and acyclic
- For
vertices, a tree always has edges - Unless otherwise specified, graphs are undirected
Bit of an Extra Notion in Computer Science
In mathematical graph theory, a tree is an undirected graph.
However, in computer science, a tree is usually deemed to be an ordered (rooted) and directed graph data structure, always having a path from the root to leaves.
Forest
A forest is a disjoint set of trees.
Sparse and Dense Graphs
Density of Graphs
Let
We define density
The density of undirected graph is:
The density of directed graph is:
Roughly speaking:
- Sparse Graph:
- The density
Basically, there is a reasonable number of edges for the number of vertices.
- The density
- Dense Graph:
- The density
As graph approaches a complete graph,
. - The density