
- Graph Theory - Home
- Graph Theory - Introduction
- Graph Theory - History
- Graph Theory - Fundamentals
- Graph Theory - Applications
- Types of Graphs
- Graph Theory - Types of Graphs
- Graph Theory - Simple Graphs
- Graph Theory - Multi-graphs
- Graph Theory - Directed Graphs
- Graph Theory - Weighted Graphs
- Graph Theory - Bipartite Graphs
- Graph Theory - Complete Graphs
- Graph Theory - Subgraphs
- Graph Theory - Trees
- Graph Theory - Forests
- Graph Theory - Planar Graphs
- Graph Theory - Hypergraphs
- Graph Theory - Infinite Graphs
- Graph Theory - Random Graphs
- Graph Representation
- Graph Theory - Graph Representation
- Graph Theory - Adjacency Matrix
- Graph Theory - Adjacency List
- Graph Theory - Incidence Matrix
- Graph Theory - Edge List
- Graph Theory - Compact Representation
- Graph Theory - Incidence Structure
- Graph Theory - Matrix-Tree Theorem
- Graph Properties
- Graph Theory - Basic Properties
- Graph Theory - Coverings
- Graph Theory - Matchings
- Graph Theory - Independent Sets
- Graph Theory - Traversability
- Graph Theory Connectivity
- Graph Theory - Connectivity
- Graph Theory - Vertex Connectivity
- Graph Theory - Edge Connectivity
- Graph Theory - k-Connected Graphs
- Graph Theory - 2-Vertex-Connected Graphs
- Graph Theory - 2-Edge-Connected Graphs
- Graph Theory - Strongly Connected Graphs
- Graph Theory - Weakly Connected Graphs
- Graph Theory - Connectivity in Planar Graphs
- Graph Theory - Connectivity in Dynamic Graphs
- Special Graphs
- Graph Theory - Regular Graphs
- Graph Theory - Complete Bipartite Graphs
- Graph Theory - Chordal Graphs
- Graph Theory - Line Graphs
- Graph Theory - Complement Graphs
- Graph Theory - Graph Products
- Graph Theory - Petersen Graph
- Graph Theory - Cayley Graphs
- Graph Theory - De Bruijn Graphs
- Graph Algorithms
- Graph Theory - Graph Algorithms
- Graph Theory - Breadth-First Search
- Graph Theory - Depth-First Search (DFS)
- Graph Theory - Dijkstra's Algorithm
- Graph Theory - Bellman-Ford Algorithm
- Graph Theory - Floyd-Warshall Algorithm
- Graph Theory - Johnson's Algorithm
- Graph Theory - A* Search Algorithm
- Graph Theory - Kruskal's Algorithm
- Graph Theory - Prim's Algorithm
- Graph Theory - Borůvka's Algorithm
- Graph Theory - Ford-Fulkerson Algorithm
- Graph Theory - Edmonds-Karp Algorithm
- Graph Theory - Push-Relabel Algorithm
- Graph Theory - Dinic's Algorithm
- Graph Theory - Hopcroft-Karp Algorithm
- Graph Theory - Tarjan's Algorithm
- Graph Theory - Kosaraju's Algorithm
- Graph Theory - Karger's Algorithm
- Graph Coloring
- Graph Theory - Coloring
- Graph Theory - Edge Coloring
- Graph Theory - Total Coloring
- Graph Theory - Greedy Coloring
- Graph Theory - Four Color Theorem
- Graph Theory - Coloring Bipartite Graphs
- Graph Theory - List Coloring
- Advanced Topics of Graph Theory
- Graph Theory - Chromatic Number
- Graph Theory - Chromatic Polynomial
- Graph Theory - Graph Labeling
- Graph Theory - Planarity & Kuratowski's Theorem
- Graph Theory - Planarity Testing Algorithms
- Graph Theory - Graph Embedding
- Graph Theory - Graph Minors
- Graph Theory - Isomorphism
- Spectral Graph Theory
- Graph Theory - Graph Laplacians
- Graph Theory - Cheeger's Inequality
- Graph Theory - Graph Clustering
- Graph Theory - Graph Partitioning
- Graph Theory - Tree Decomposition
- Graph Theory - Treewidth
- Graph Theory - Branchwidth
- Graph Theory - Graph Drawings
- Graph Theory - Force-Directed Methods
- Graph Theory - Layered Graph Drawing
- Graph Theory - Orthogonal Graph Drawing
- Graph Theory - Examples
- Computational Complexity of Graph
- Graph Theory - Time Complexity
- Graph Theory - Space Complexity
- Graph Theory - NP-Complete Problems
- Graph Theory - Approximation Algorithms
- Graph Theory - Parallel & Distributed Algorithms
- Graph Theory - Algorithm Optimization
- Graphs in Computer Science
- Graph Theory - Data Structures for Graphs
- Graph Theory - Graph Implementations
- Graph Theory - Graph Databases
- Graph Theory - Query Languages
- Graph Algorithms in Machine Learning
- Graph Neural Networks
- Graph Theory - Link Prediction
- Graph-Based Clustering
- Graph Theory - PageRank Algorithm
- Graph Theory - HITS Algorithm
- Graph Theory - Social Network Analysis
- Graph Theory - Centrality Measures
- Graph Theory - Community Detection
- Graph Theory - Influence Maximization
- Graph Theory - Graph Compression
- Graph Theory Real-World Applications
- Graph Theory - Network Routing
- Graph Theory - Traffic Flow
- Graph Theory - Web Crawling Data Structures
- Graph Theory - Computer Vision
- Graph Theory - Recommendation Systems
- Graph Theory - Biological Networks
- Graph Theory - Social Networks
- Graph Theory - Smart Grids
- Graph Theory - Telecommunications
- Graph Theory - Knowledge Graphs
- Graph Theory - Game Theory
- Graph Theory - Urban Planning
- Graph Theory Useful Resources
- Graph Theory - Quick Guide
- Graph Theory - Useful Resources
- Graph Theory - Discussion
Graph Theory - Treewidth
Treewidth
Treewidth in graph theory measures how close a graph is to being a tree. It is a numerical value assigned to a graph that represents its complexity in terms of tree-like structures −
- A tree has a treewidth of 1 because you can represent it with bags containing only two vertices (one vertex plus its neighbor).
- A graph with many cycles or a dense structure has a higher treewidth.
The treewidth of a graph G is the minimum width among all tree decompositions of G. A tree decomposition is a way of representing a graph as a tree, where −
- Each node of the tree corresponds to a subset of the vertices of the graph, called a "bag."
- For each edge in the graph, there must be at least one bag in the tree containing both endpoints of the edge.
- If a vertex appears in multiple bags, all those bags must form a connected subtree.
The width of a tree decomposition is the size of the largest bag minus one. The treewidth of the graph is the minimum width over all possible tree decompositions.
For example, consider a cycle graph Cn with n ≥ 4 −
- If you remove one edge from the cycle, it becomes a tree, so the treewidth is 1.
- For the original cycle graph, the treewidth is 2, because every tree decomposition will need at least 3 vertices in some bag to include all edges.

Importance of Treewidth
Treewidth plays an important role in graph algorithms and helps solve problems more efficiently. Following are its major uses −
- Better Algorithm: Many hard problems become easier to solve on graphs with small treewidth.
- Graph Approximation: Treewidth helps in approximating solutions for problems in real-world networks and systems.
- Dynamic Programming: Tree decompositions let us use dynamic programming on graphs.
- Real-world Applications: It is used in database systems, computer vision, and artificial intelligence to make computations faster.
Tree Decompositions and Treewidth
The concept of treewidth is closely realted to tree decomposition, which represents a graph as a tree-like structure. The treewidth of a graph is defined as one less than the size of the largest set of nodes in a tree decomposition.
A tree decomposition of a graph satisfies the following −
- Each node of the graph must be part of at least one "bag" in the tree decomposition.
- If two nodes are connected by an edge, there must be at least one "bag" containing both nodes.
- The nodes present in a bag must form a connected subtree of the decomposition.
Example of Treewidth Calculation
Consider the following graph −
Graph: Nodes = [A, B, C, D, E] Edges = [(A, B), (B, C), (C, D), (B, D), (D, E)]
A tree decomposition of this graph looks like this −
Tree Decomposition: Bag 1: [A, B] Bag 2: [B, C] Bag 3: [B, D] Bag 4: [D, E]
The size of the largest bag is 3, so the treewidth is −
Treewidth = 3 - 1 = 2

Properties of Treewidth
Graphs with small treewidth have the following properties −
- Tree-like Structure: Graphs with low treewidth look similar to trees.
- Efficient Algorithms: Many graph algorithms work quickly on graphs with low treewidth.
- Decomposition Quality: A graph can have multiple tree decompositions, and the treewidth depends on the best decomposition.
Graphs with Bounded Treewidth
Some graph classes have bounded treewidth as follows −
- Trees: A tree has a treewidth of 1.
- Cycles: A cycle graph with n nodes has a treewidth of 2.
- Planar Graphs: Planar graphs can have higher treewidth, but many subclasses have bounded treewidth.

Treewidth Calculation for the Tree
- The tree has only 3 nodes (A, B, C), with edges (A, B) and (B, C).
- The decomposition has bags like [A, B], [B, C].
- The largest bag has size 2, and the treewidth is 2 - 1 = 1.
Treewidth Calculation for the Cycle
- The cycle graph here has 5 nodes. We can decompose it into bags like [0, 1], [1, 2], [2, 3], [3, 4], and [4, 0].
- Each bag contains 2 nodes that overlap with adjacent bags, ensuring a proper decomposition.
- The largest bag has size 2, so the treewidth is 2 - 1 = 2.
Treewidth Calculation for the Planar Graph
- The planar graph here has 5 nodes and 5 edges, forming a simple structure.
- A tree decomposition might look like [A, B], [B, C], [C, D], [D, A], [A, E], with a treewidth greater than 2 due to the complexity of the graph's structure.
- The treewidth here is calculated by finding the size of the largest bag and subtracting 1. The largest bag in this decomposition has size 3 (for example, [A, B, C]), so the treewidth is 3 - 1 = 2.
Treewidth and Computational Problems
Treewidth is useful in solving difficult computational problems. The following list shows some tough problems that are easier to solve on graphs with small treewidth −
- Graph Coloring: It becomes easier to find solutions on graphs with low treewidth.
- Shortest Path: Tree decompositions allow faster ways to find the shortest path.
- Hamiltonian Path: This problem can be solved in less time for graphs with small treewidth.
Example: Algorithm using Treewidth
Let us compute the shortest path using tree decompositions −
# Graph Nodes = ["A", "B", "C", "D", "E"] Edges = [("A", "B"), ("B", "C"), ("C", "D"), ("D", "E")] # Tree Decomposition Tree_Decomposition = { 1: ["A", "B"], 2: ["B", "C"], 3: ["C", "D"], 4: ["D", "E"] }
Now, let us create a simple dynamic programming algorithm to compute the shortest path on this graph using the tree decomposition. We will use the decomposition to break the problem down into subproblems and compute the shortest path for each node in each bag −
# Initialize a dictionary to store shortest path values Shortest_Path = {} # Iterate through each bag in the tree decomposition for bag in Tree_Decomposition.values(): # For each node in the current bag, compute the shortest path for node in bag: # Update the shortest path for the node, considering all possible previous nodes Shortest_Path[node] = min(Shortest_Path.get(node, float("inf")), ...) # Output the computed shortest path for each node print(Shortest_Path)