Академический Документы
Профессиональный Документы
Культура Документы
Lecture 5: Graphs
Graphs
A graph G = (V, E) consists of a set of
vertices V, and a set of edges E, such that
each edge in E is a connection between a
pair of vertices in V.
The number of vertices is written |V|, and the
number edges is written |E|.
Graphs (2)
Connected Components
A subgraph S is formed from a graph G = (V,
E) by selecting a subset V_s of V a subset E_s
of E such that for every edge e in E_s, both of
es vertices are in V_s.
An undirected graph is connected if there is at least
one path from any vertex to any other.
The maximum connected subgraphs of an
undirected graph are called connected components.
Acyclic graphs
A graph without cycles is called
acyclic.
Thus, a directed graph without cycles
is called a directed acyclic graph or
DAG.
A free tree is a connected, undirected
graph with no simple cycles.
An equivalent definition is that a free
tree is connected and has |V|-1 edges.
Started on 09 April 2016
Directed Representation
Undirected Representation
Comparison
Adjacency List
Stores information only for
those edges that actually
appear in the graph
Needs pointers.
Adjacency Matrix
Requires space for each
potential edge, whether it
exists or not
Requires no overhead for
pointers, which can be a
substantial cost, especially if
the only information stored
for an edge is one bit to
indicate its existence.
Sparse graphs are likely to However, As the graph
have their adjacency list becomes
denser,
the
representation
be
more adjacency matrix becomes
space efficient.
relatively
more
space
Course Teacher: Prof. Sohel
efficient.
Started on 09 April 2016
10
Rahman
11
Graph ADT
We assume that
vertices are defined by an integer index
value.
there is a Vertex 0, Vertex 1, and so on
C++3elatestP387.pdf
12
Matrix Implementation
C++3elatestP389-390.pdf
13
List Implementation
C++3elatestP391-392.pdf
14
Graph Traversals
Some applications require visiting every
vertex in the graph exactly once.
The application may require that vertices be
visited in some special order based on
graph topology.
Examples:
15
16
17
18
19
20
DFS Cost
DFS processes each edge once in a
directed graph.
In an undirected graph, DFS
processes each edge from both
directions.
Each vertex must be visited, but only
once.
So the total cost is: Cost: (|V| + |E|).
Started on 09 April 2016
21
C++3elatestP395.pdf
22
23
24
25
C++3elatestP397.pdf
26
27
28
29
30
31
Topological Sorting
Example
Started on 09 April
2016
32
Topological Sorting
Example
9
Started on 09 April
2016
33
Topological Sorting
Example
8
9
Started on 09 April
2016
34
Topological Sorting
Example
7
8
9
Started on 09 April
2016
35
Topological Sorting
Example
7
8
9
Started on 09 April
2016
36
Topological Sorting
Example
7
8
9
Started on 09 April
2016
37
Topological Sorting
Example
4
6
7
8
9
Started on 09 April
2016
38
Topological Sorting
Example
3
4
6
7
8
9
Started on 09 April
2016
39
Topological Sorting
Example
2
3
4
6
7
8
9
Started on 09 April
2016
40
Topological Sorting
Example
2
3
4
6
7
8
9
Started on 09 April
2016
41
Queue-Based Topsort
First visit all edges, counting the number of edges that
lead to each vertex
i.e., count the number of prerequisites for each vertex
Queue-Based Topsort
43