Graph
On this page
Concepts
- Vertex/Node
- degree
- Edge/Arc
- directed
- undirected
- weighted
- Connectivity
- path between two vertices
- length
- distance between two vertices
- adjacent
- connected
- cyclic
- acyclic
- path between two vertices
- Graph properties
- directed/undirected
- weighted/unweighted
- cyclic/acyclic
- connected/unconnected
- dense/sparse
Examples
Directed connected cyclic graph
Undirected connected cyclic weighted graph
Undirected connected acyclic graph (Tree)
Directed acyclic graph (DAG) task planning
Representations
Use our Graph Representation Lab to compare the same graph as an edge list, adjacency list, and adjacency matrix.
For the undirected graph:
Undirected graph
-
List of edges
A list of edges is a 1d array in which every element in the array represents an edge. It is the simplest way to represent a graph. It is also an easy way to store a graph in a relational database. It is not efficient for most graph algorithms though.
1 2 1 3 1 4 3 4 3 5 4 5 -
Adjacency list
An adjacency list is a 1d array in which every element in the array represents a vertex and points to a list of its neighbors.
Adjacency list
-
Adjacency matrix
An adjacency matrix is a 2d array in which each row represents a source vertex and each column represents a destination vertex. The elements in the matrix represents the edge between the two vertices.
Adjacency Matrix
_ 1 2 3 4 5 1 0 1 1 1 0 2 1 0 0 0 0 3 1 0 0 1 1 4 1 0 1 0 1 5 0 0 1 1 0
Graph Traversal
-
Depth-first search (DFS)
Starting from one vertex, visit all vertices along the path started from the starting vertex till then end of the path (no unvisited neighbors) and then backtrack.
- iterative using a stack
- recursive
-
Breadth-first search (BFS)
Starting from one vertex, visit all other vertices in the order of the distances to the starting vertex.
- iterative using a queue
Try our BFS & DFS Graph Explorer to step through the queue, recursive call stack, discovery order, and distances.
Algorithms
Minimum spanning tree
This algorithm generates a tree that connects all connected vertices in a graph with the minimum total edge weight. See visualization
- Prim’s algorithm
- Kruskal’s algorithm
Dijkstra’s shortest path
Among the many variations of the shortest path algorithms, single-source shortest path algorithm is most commonly discussed. The objective is to find the shortest path of all other vertices to given a vertex (source). Shortest path problem with unweighted graph can be solved using BFS. Dijkstra’s approach works with weighted (non-negative weight) graph. Try our Dijkstra Shortest Paths Visualizer, then see visualgo.net for another visualization.
- invented by Edsger Dijkstra
- employs a priority queue ADT
Graph Glossary
- Vertex/Node (Graph)
- Entities that are interconnected
- Edge/Arc (Graph)
- The connection between two vertices
- Adjacency
- Two vertices are adjacent if connected by an edge
- Neighbors (Graph)
- All vertices that are adjacent
- Path (Graph)
- A sequence of edges from one vertex to another
- Length of path (Graph)
- The number of edges along a path
- Distance (Graph)
- The length of the shortest path between two vertices
- Full graph
- A graph in which all vertices are connected to other vertices
- Sparse graph
- A graph in which the number of edge is way fewer than the number for a full graph with the same number of vertices
- Directed graph
- A graph in which every edge has a direction from one vertex to another
- Undirected graph
- A graph in which every edge is symmetric
- Cyclic Graph
- A graph in which you can always find at least a path that starts and ends on a same vertex
- Acyclic Graph
- Can not find any path that starts and ends on a same vertex
- Weighted graph
- A graph in which every edge has a weight
- Depth-first search (DFS, Graph)
- Starting from one vertex, visit all vertices along the path started from the starting vertex till then end of the path (no unvisited neighbors) and then backtrack.
- Breadth-first search (BFS)
- Starting from one vertex, visit all other vertices in the order of the distances to the starting vertex.