Data structures - Graphs 1

GRAPHS
Mathematically, a graph is a set V of vertices and a set E of edges, such that each edge in E connects two of the vertices in Y.
The term node is also used here as a synonym for vertex.

Two common representations of graphs:
the adjacency matrix representation
the adjacency list representation

GRAPH TERMINOLOGY
在這裏插入圖片描述
The length of a path is the n}unher of edges on the path.
A graph is connected if there is a path from each vertex to every other vertex.
A graph is complete if there an edge from each vertex to every other vertex.
在這裏插入圖片描述
The degree of a vertex is equal to the number of edges connected to it.
The degree of each vertex in a complete graph is equal to the number of vertices minus one.
A subgraph of a given graph consists of a subset of that graph’s vertices and the edges connecting those vertices.
A connected component is a subgraph consisting of the set of vertices that are reachable from a given vertex.

在這裏插入圖片描述
A simple path is a path that does not pass through the same vertex more than once.
A cycle is a path that begins and ends at the same vertex.
在這裏插入圖片描述

在這裏插入圖片描述

A connected graph that has relatively many edges is called a dense graph, whereas one that has relatively few edges is called a sparse graph.
There are two limiting cases.
The number of edges in a complete directed graph with N vertices is N*(N一1), and the number of edges in a complete undirected graph is N * (N一1)/2.
Thus, the limiting case of a dense graph has approximately N*N edges. By contrast, the limiting case of a sparse graph has approximately N edges.

REPRESENTATIONS OF GRAPHS
Adjacency Matrix
在這裏插入圖片描述
在這裏插入圖片描述
Adjacency List
Assume that a graph has N vertices labeled 0, 1,…, N一1, and then the following applies:
·The adjacency list for the graph is an array of N linked lists.
·The ith linked list contains a node for vertex j if and only if there is an edge from vertex i to vertex j.
在這裏插入圖片描述
Analysis of the Two Representations
There are two operations following:
~ Determine whether or not there is an edge between two given vertices.
~ Find all the vertices adjacent to a given vertex.

The adjacency matrix supports the first operation in constant time because it requres just an index operation into a two一dimensional array.
The adjacency list tends to support the second operation more efficiently than the adjacency matrix.

GRAPH TRAVERSALS
A Generic Traversal Algorithm
在這裏插入圖片描述
In the foregoing function, for a graph that contains N vertices, the following applies:

  1. All vertices reachable from startVertex are processed exactly once.

  2. Determining all the vertices adjacent to a given vertex is straightforward:
    a. When an adjacency matrix is used, you iterate across the row corresponding to the vertex.
           This is an O(N)operation.
           Repeating this for all rows is O(N*N).

    b. When an adjacency list is used, you traverse the vertex’s linked list.
           Performance depends on how many vertices are adjacent to the given vertex.
           Repeating this for all vertices is O(max(M,N)),where M is the number of edges.

Breadth一First and Depth一First Traversals
在這裏插入圖片描述
depth-first
Implement a depth-first traversal recursively.
在這裏插入圖片描述
traverse all the vertices of an undirected graph component by component the iterative version:
在這裏插入圖片描述
the recursive version:
在這裏插入圖片描述
在這裏插入圖片描述
Graph Components
partition the vertices of a graph into disjointed components.
在這裏插入圖片描述
TREES WITHIN GRAPHS
Algorithms for Minimum Spanning Trees
Prim’s algorithm
在這裏插入圖片描述
modify the algorithm slightly:
在這裏插入圖片描述
在這裏插入圖片描述
TOPOLOGICAL SORT
A topological order assigns a rank to each vertex such that the edges go from lower to higher-ranked vertices.
在這裏插入圖片描述
在這裏插入圖片描述
The process of finding and returning a topological order of vertices in a graph is called a topological sort. One topological sort algorithm is based on a graph traversal.

Use a depth一first traversal
在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章