Академический Документы
Профессиональный Документы
Культура Документы
Directed Graph
• A graph with only directed edges is said to be
directed graph.
Mixed Graph
• A graph with undirected and directed edges is
said to be mixed graph.
End vertices or Endpoints
The two vertices joined by an edge are called the end
vertices (or endpoints) of the edge.
Origin
If an edge is directed, its first endpoint is said to be origin
of it.
Destination
If an edge is directed, its first endpoint is said to be origin
of it and the other endpoint is said to be the destination
of the edge.
Adjacent
Two node or vertices are adjacent if they are connected
to each other through an edge.
Outgoing Edge
• A directed edge is said to be outgoing edge on
its origin vertex.
Incoming Edge
• A directed edge is said to be incoming edge on
its destination vertex.
Degree
• Total number of edges connected to a vertex is
said to be degree of that vertex.
Indegree
• Total number of incoming edges connected to a vertex
is said to be indegree of that vertex.
Outdegree
• Total number of outgoing edges connected to a vertex
is said to be outdegree of that vertex.
Path
• Path represents a sequence of edges between the two
vertices.
Forest
• A forest is an acyclic graph (i.e., a graph without any
graph cycles)
Strongly Connected
• A directed graph is said to be strongly connected, if
every vertex is reachable from every other vertex.
• A cyclic graph is a graph containing at least one graph cycle.
A graph that is not cyclic is said to be acyclic.
Graph Representations
Following two are the most commonly used representations of a graph.
1. Adjacency Matrix
2. Adjacency List
1. Adjacency Matrix
In this representation, graph can be represented using a matrix of size total
number of vertices by total number of vertices. That means if a graph with 4
vertices can be represented using a matrix of 4X4 class. In this matrix, rows
and columns both represents vertices. This matrix is filled with either 1 or 0.
Here, 1 represents there is an edge from row vertex to column vertex and 0
represents there is no edge from row vertex to column vertex.
Adjacency Matrix
Consider the following undirected graph
representation...
Directed graph representation...
Adjacency List
• In this representation, every vertex of graph
contains list of its adjacent vertices.
Single-Source Shortest Path Problem
Dijkstra’s Algorithm
Single-Source Shortest Path Problem - The
problem of finding shortest paths from a source
vertex v to all other vertices in the graph.
Applications
- Maps (Map Quest, Google Maps)
- Routing Systems
Dijkstra's algorithm
Dijkstra's algorithm - is a solution to the single-source
shortest path problem in graph theory.
4 1 3 10
2 2 ∞
∞ C D E
5 8 ∞ 4 6
1
F G
∞ ∞
47
Example: Update neighbors'
distance
0 2
2
A B
4 1 3 10
2 2 ∞
∞ C D E
5 8 1 4 6
Distance(B) = 2 1
F G
Distance(D) = 1
∞ ∞
48
Example: Remove vertex with
minimum distance
0 2
2
A B
4 1 3 10
2 2 ∞
∞ C D E
5 8 1 4 6
1
F G
∞ ∞
49
Example: Update neighbors
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
Distance(C) = 1 + 2 = 3 1
F G
Distance(E) = 1 + 2 = 3
Distance(F) = 1 + 8 = 9 9 5
Distance(G) = 1 + 4 = 5
50
Example: Continued...
Pick vertex in List with minimum distance (B) and update neighbors
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
Note : distance(D) not
F
1
G updated since D is
already known and
9 5 distance(E) not updated
since it is larger than
previously computed
51
Example: Continued...
Pick vertex List with minimum distance (E) and update neighbors
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
1
F G
No updating
9 5
52
Example: Continued...
Pick vertex List with minimum distance (C) and update neighbors
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
Distance(F) = 3 + 5 = 8 1
F G
8 5
53
Example: Continued...
Pick vertex List with minimum distance (G) and update neighbors
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
1
F G
Previous distance
6 5
Distance(F) = min (8, 5+1) = 6
54
Example (end)
0 2
2
A B
4 1 3 10
2 2
3 C D E 3
5 8 1 4 6
1
F G
6 5
Pick vertex not in S with lowest cost (F) and update neighbors
55
Another Example
Another Example
Another Example
Another Example
Another Example
Another Example
Another Example
Another Example
Another Example