Lecture12 Graphs
Lecture12 Graphs
Graph Terminology
2
1
3
V is set of vertices
E is set of edges
1
2
Undirected Graph
edges do not have a direction
The edge from 1 to 2 is also an edge
from 2 to 1
Edge (1, 2) implies that there is also
an edge (2, 1) [ The same edge ]
3
1
2
Directed Graph
edges have a direction
Edge (2, 1) means only that there is an
edge from 2 to 1
In this example,there is no edge from 1
to 2
4
12
2
22
15
1
3
13
32
16
Weighted Graph
weights (values) are associated with the
edges in the graph
may be directed for undirected
Weighted graphs are also referred to as
networks
5
1
2
Complete Graph
For each pair of vertices, there is one
edge
If G = (V, E) is a complete graph, and
|V| = n, then can you calculate |E|?
1
2
Subgraph
A subgraph G of graph G = (V, E) is a
graph (V, E) that V V and E E.
Path
the sequence of edges (i1, i2), (i2,
i3), ,(ik-1, ik).
Denoted as path i1, i2, ..., ik
Simple path all vertices (expect
possibly first and last) are different
Length of path is sum of the lengths of
the edges
Examples of graph
Representation of Graphs
Adjacency matrix
Incidence matrix
Adjacency lists: Table, Linked List
Space/time trade-offs depending on
which operation are most frequent as
well as properties of the graph
10
11
12
13
14
15
16
17
18
An Example
1
20
Algorithm
An Example
3
1
2
4
5
7
23
24
25
26
8
5
2
6
8
5
4
1
10
10
5
4
1
6
27
0
1
2
3
2
4
8
5
10
4
1
2
2
2
1
4
8
5
10
4
1
6
28
2
2
2
1
4
8
5
10
4
1
29
Select v2 as known
0
1
3 3
2
2
1
4
10
5 3
6
1
6
7
9
5
Check v4, v5. v4 is final, v5 has lower cost of 3 vs. 12
0
2
2
1
2
10
4
3
1 1
2
2
4
5 3
3 3
8
4
5
6
1
6
7
9
5
30
Select v5 as known
0
1
3 3
2
2
2
1
4
1
6
9
Check v7, but not adjusted
0
1
2
2
1
4
8
5
6
9
5 3
6
7
5
3 3
10
10
5 3
4
1
7
5
6
31
v3 is selected as known
0
1
3 3
2
2
1
4
10
5 3
6
1
6
7
9
5
Check v1 and v6. v1 known, v6 adjusted from 9 to 8
0
2
2
1
2
10
4
1
3
1
2
2
4
5 3
3 3
8
4
5
6
1
6
7
8
5
32
Select v7 as known
0
1
4
1
2
3 3
2
2
2
1
4
8
5
6
8
10
5 3
4
1
7
5
2
2
10
2
7
5
5 3
6
33
Select v6 as known
0
1
4
1
2
3 3
2
2
2
1
4
8
1
Done
6
6
5 3
2
2
2
1
7
5
1
4
8
5
6
6
Nothing to check
0
1
4
2
3 3
10
10
5 3
4
1
7
5
6
34
Spanning tree
subgraph of G
contains all vertices of G
connected graph with no cycles
35
36
Kruskal's algorithm
Dijkstras method
Prim's algorithm
Kruskal's algorithm
KruskalAlgorithm( weighted connected
undirected graph)
tree = null;
edges = sequence of all edges of graph
sorted by weight;
for(i=1;i<= |E| and |tree| < |V|-1; i++)
if e i from edges does not form a cycle with
edges in tree
add e i to tree;
38
39
8
5
2
3
10
4
1
6
40
8
5
2
3
10
4
1
6
41
8
5
2
3
10
4
1
6
42
8
5
2
3
10
4
1
6
43
Dijkstras method
DijkstraMethod( weighted connected undirected
graph)
tree = null;
edges = an unsorted sequence of all edges of
graph;
for j=1 to |E|
add e i to tree;
if there is a cycle in tree
remove an edge with maximum
weight from this only cycle;
44
45
Prim's algorithm
Similar to the Dijkstras algorithm in
shortest paths
grow the tree in successive stages
in each stage, one node is picked as the
root, we add an edge, and thus a vertex
is added to the tree
have a set on vertices in the tree and a
set that is not in the tree
46
47
8
5
4
8
4
1
10
10
4
1
6
48
8
5
4
8
4
1
10
10
4
1
6
49
8
5
4
8
4
1
10
10
4
1
6
50
4
8
10
4
1
6
51