algorithm-notes
  • Preface
  • Outline
    • Asymptotic-Analysis
    • Divide-and-Conquer (DnC)
      • Overview
      • Multiplication
      • DnC Strategy
      • Master Method
    • Randomization
      • Overview
      • Discrete Probability
      • Randomized Algorithms
      • Reservoir Sampling
    • Sorting
      • Overview
      • Selection Sort
      • Bubble Sort
      • Insertion Sort
      • Shell Sort
      • Merge Sort
      • Heap Sort
      • Quick Sort
      • Non-Comparison Sort
    • Graph Algorithms
      • Overview
      • Minimum Spanning Tree
      • Shortest Path
      • Topological Sort
    • Tree
      • Overview
      • Balanced Search Tree
        • AVL Tree
        • Red-Black Tree
        • B Tree
      • Heap
      • Disjoint Set
      • Threaded Binary Tree
      • Segment Tree
    • Searching
      • Overview
      • Binary Search
      • Graph Search
      • Hash Table
      • String Search
    • Dynamic Programming
      • Overview
      • Fibonacci Numbers
      • LCS problem
    • Greedy Algorithms
      • Overview
Powered by GitBook
On this page
  • Graph Definitions
  • Directed Graph
  • Undirected Graph
  • Degrees
  • Connectivity
  • Graph Representations
  • Adjacency Matrix
  • Adjacency List
  • Table of Contents

Was this helpful?

  1. Outline

Graph Algorithms

PreviousNon-Comparison SortNextOverview

Last updated 6 years ago

Was this helpful?

is a study of graphs, a mathematical structure modeling the associations between objects. It is vital for many computer science problems and worth attention for not only theoretical studies but also practical application development.

This chapter discusses fundamental graph structures, typical graph problems and correlated algorithm analysis. Specifically, graph searching primitives such as DFS and BFS are the most important techniques throughout the graph algorithms.

Graph Definitions

Generally, there are two kinds of graphs: and . Both of them involve a relationship between Vertex V and Edge E.

Directed Graph

Given a graph G = (V, E), wherein the V is a set of vertices of G and E is a set of edges going in and out from vertices with directions, connected them together. Specifically, given vertices {1, 2, 3, 4, 5, 6}, a graph can be built like below:

Figure 1. Directed Graph

Note: there are cycles (even self cycles, a vertex points to itself) in directed graphs and to be noted that problems like could not have solutions if cycles present, the directed graph with no cycles is called DAG (directed acyclic graph).

Undirected Graph

Unlike , undirected graph only has edges with no directions between connected vertices. Since the relations among vertices have no ordering, there exists no self cycles. Similarly,

Figure 2. Undirected Graph

Degrees

In an , the degree of a vertex means the number of edges associated with that vertex. For instance, the degree of vertex 3 in the above graph is 3;

While in the , the degree of a vertex is the summation of its in-degree and out-degree. Such in-degree means the number of edges pointing towards the vertex and out-degree means the number of edges pointing out of the vertex.

Note: the vertex with zero degree means it is a lonely island, disconnected from any other vertices in the graph.

Connectivity

Substituting all directional edges of a directed graph with undirected ones could obtain a base graph of the original graph. If such a base graph is connected, then the original directed graph is weakly connected.

Graph Representations

Adjacency Matrix

This matrix (e.g. adj[][]) is a 2D array with size |V| ✗ |V|; In an undirected graph, if vertex a and vertex b are connected, then adj[a][b] = 1 and adj[b][a] = 1; if there are no edges between say vertex e and vertex c, then adj[e][c] = 0, adj[c][e] = 0;

For a directed graph, the matrix cell value depends on the existance of directional edges; If vertex a has an edge going outward to vertex b while vertex b has no edges going inward to vertex a, then adj[a][b] = 1 but adj[b][a] = 0

Adjacency matrix is practical when the graph is dense (at least one edge going in between two vertices) and finding the existence of edges is immediate (LOOKUP costs Ο(1)). It is less so if the graph is sparse (the graph topology structure approaches a forest rather than a graph).

Adjacency List

Though it normally costs Ο(|E|) time to LOOKUP edge associations, it is a preferable choice than adjacency matrix in a massive-scale data network such as WEB.

Table of Contents

If for any given vertices in an , there exists paths reachable from all other vertices, then the graph is connected. Specifically, if an undirected graph G = (V, E) is connected, the number of edges is larger than or equal to number of vertices minus 1: |E| ≥ |V| - 1.

There are only definitions of strongly connected and weakly connected in a . In a strongly connected directed graph G = (V, E), for any two distinct vertices x, y within V, there exists paths from x to y and from y to x;

Two vertices sharing a same edge are adjacent in a graph. To define such relationships among vertices, two practical uses of data structures are introduced: and .

Figure 3. Adjacency Matrices of a Directed Graph and an Undirected Graph

Adjacency list is an array of linkedlist in design and it costs only Ο(|V| + |E|) compared to . In addition, the array can be dynamic which means adding vertices to a graph is easier than a static structure like adjacency matrix.

Figure 4. Adjacency List of a Directed Graph

- a searching primitive prototype for many important algorithms like [Minimum Spanning Tree] and [Dijkstra Shortest Path].

- in contrast to BFS, DFS provides a simple recursive implementation.

- a prevalent type of graph algorithm in practice, e.g. find a shortest path from location A to location B in map.

- a technique to transform graphs to trees

- split the graph into different groups for applications like .

- sort a graph with respect to its topological relations.

Breadth-First Search
Depth-First Search
Shortest Path Algorithms
Minimum Spanning Tree
Graph Minimum-Cut
community finding
undirected graph
directed graph
adjacency matrix
adjacency list
adjacency matrix
Topological Sort
DAG
Graph Theory
topological sort
directed graph
undirected graph
directed graph
undirected graph
directed graph