Professional Documents
Culture Documents
Graphs
Prepared by Kristian Guillaumier
Department of Intelligent Computer Systems
Revision: Trees
Simple binary tree from Wikipedia:
Binary Tree
class Node
{
public int Value
public Node LeftChild
public Node RightChild
}
= 0;
= null;
= null;
Binary Tree
static Node CreateNode(int value)
{
Node newNode
= new Node();
newNode.Value
= value;
newNode.LeftChild
= null;
newNode.RightChild = null;
//
return newNode;
}
Binary Tree
static void Main(string[] args)
{
Node root
=
root.LeftChild
=
root.RightChild
=
root.LeftChild.LeftChild
=
root.LeftChild.RightChild
=
root.RightChild.LeftChild
=
CreateNode(10);
CreateNode(3);
CreateNode(44);
CreateNode(11);
CreateNode(8);
CreateNode(99);
Console.ReadLine();
}
10
11
12
13
14
15
16
17
18
19
20
21
23
24
LLRotate
static void LLRotate(Node node)
{
Node temp
=
node.RightChild
=
node.LeftChild
=
node.RightChild.LeftChild
=
node.RightChild.RightChild =
//
int tempVal
node.Value
node.RightChild.Value
node.RightChild;
node.LeftChild;
node.RightChild.LeftChild;
node.RightChild.RightChild;
temp;
= node.Value;
= node.RightChild.Value;
= tempVal;
25
RR Rotation
26
RRRotate
static void RRRotate(Node node)
{
Node temp
=
node.LeftChild
=
node.RightChild
=
node.LeftChild.RightChild
=
node.LeftChild.LeftChild
=
//
int tempVal
node.Value
node.LeftChild.Value
node.LeftChild;
node.RightChild;
node.LeftChild.RightChild;
node.LeftChild.LeftChild;
temp;
= node.Value;
= node.LeftChild.Value;
= tempVal;
27
LR Rotation
28
RL Rotation
29
LRRotate/RLRotate
static void LRRotate(Node node)
{
RRRotate(node.LeftChild);
LLRotate(node);
}
static void RLRotate(Node node)
{
LLRotate(node.RightChild);
RRRotate(node);
}
30
31
Graphs
Some graph notes and examples are taken from:
http://www.brpreiss.com/books/opus6/html/page522
.html
ADT:
Collection of nodes/vertices.
Nodes are connected by edges/arcs.
G = (V, E).
E = (V1, V2).
Example Operations:
Finding a path between nodes.
32
Basics
Ordered pair:
Set:
A sequence of elements:
Order and repetitions are irrelevant.
{a, b} = {b, a} = {b, a, b}.
33
Directed Graphs
Ordered pair G = (V, E)
V: finite, non-empty set of vertices.
E: finite, non-empty set of ordered pairs called
edges.
EVV
Example
G = (V1, E1)
Where:
V1 = {a, b, c, d}
E1 = {(a,b), (a,c), (b,c) }
Giving:
35
Basics
Sometimes an edge (v, w) E is written v w.
The arrow is called a directed arc.
w is the head of the arc.
v is the tail of the arc.
Basics
Order of a graph:
|V| = number of vertices.
Size of a graph:
|E| = number of edges.
Out-Degree of a vertex:
The number of edges emanating from a node.
Written as |A(v)|
In-Degree of a vertex:
The number of edges incident on a node.
Written as |I(v)|
37
Basics
38
Basics
Inverted edges:
If (u, v) is an edge e1 then e2 = (v, u) is e1
inverted.
Pseudograph/Multigraph:
Two vertices may be connected by more than one
edge e.g. vertices a and b and edges ((b,a), (b,a)).
39
Walks
A walk in a graph G is a non-empty sequence of
vertices:
P = {v1, v2, v3, , vk}
The length of a walk is the number of edges in the
walk.
Open walk: start and end vertices are not the same.
Closed walk: start and end vertices are the same.
The length of an open walk is (n-1) where n is the
number of vertices visited.
The length of a closed walk is (n) where n is the
number of vertices visited.
40
Basics
Each vertex vi in a walk/path has a successor vi+1 except
the last.
Each vertex vi in a walk/path has a predecessor vi-1
except the first.
A walk is simple iff vi != vj such that:
1 <= i <= j <= k.
It is permissible for v1 = vk. In this case the walk is closed.
Otherwise it is an open walk.
Paths
A path is a simple walk (no vertex is repeated).
May be open or closed.
42
Definitions
Cycle:
Closed path no vertex is repeated except start and
end vertices. v1 = vk.
The length of a cycle is the length of the path.
Loops:
A loop is a cycle whose length = 1 i.e. {v,v}
Acyclic:
A graph without cycles.
A tree is a connected, directed, simple, acyclic graph.
43
Basics
Subgraph:
The subgraph S of a graph G is one where:
Each vertex in S is a vertex in V of G.
Each edge in S:
Is a subset of the edges in G.
Each vertex in the edges of S is a vertex in S.
Connected graph:
Every pair of vertices is connected by an edge.
44
Definitions
Undirected graphs:
An edge is not an ordered pair but a set of 2
vertices.
Directed Graph/Digraph:
D = (V, E), such that:
D = set of vertices.
E = ordered pair of vertices.
46
Undirected Graphs
Vertices are connected by undirected arcs:
No arrow.
No head or tail.
An edge is a set not an ordered pair.
{v,w} = {w,v}.
E.g. V={a,b,c,d}, E={{a,b}, {a,c},{b,c},{c,d}}
47
48
49
Basics
Complete Graph:
Simple graph (no loops).
Any pair of vertices is connected by an edge.
G complete graph with n vertices has n(n-1)/2 edges.
Planar Graph:
Can be drawn on a plane without any edges
intersecting.
Applications:
Wiring microprocessor circuits.
50
Graph Representation
Note that in a directed graph G=(V,E),
We can have |V|2 possible edges:
1 vertex = 1 loop.
2 vertices = 1 loop, 1 normal, 1 normal, 1 loop.
51
Graph Representation
Adjacency Matrix:
Consider G=(V,E), V=(v1,v2,vn).
We can use a n by n matrix A of 1s and 0s where:
Ai,j = 1 if (vi,vj) E,
0 otherwise.
52
Graph Representation
Common representations are the adjacency list and the
adjacency matrix.
Suppose the vertices of a graph are labeled with
numbers from 0 to (n-1).
Adjacency matrix:
An adjacency matrix would then be a 2D array (n by n) of
Boolean values.
A[i, j] true if there is an edge from i to j.
If the graph is weighted, then a value in the matrix is the
weight.
If the graph is undirected A[i, j] = A[j, i]. This will make the
matrix symmetric about the diagonal.
53
Graph Representation
In adjacency matrices, space is O(|V|2).
Irrespective of the number of edges in the
graph.
If |E| << |V|2 then the matrix will be sparse
and will be inefficient most of it will be
zeros.
Sparse vs dense.
54
Dense/Sparse Graphs
Dense: number of edges is close to the
maximum number of edges.
A complete graph is the most dense.
A sparse graph is the opposite.
Which implementation is best for a sparse
graph:
Hint: An adjacency matrix always has n2 elements.
55
Graph Representation
Adjacency list:
Linked list of linked lists.
For a graph with n vertices, the primary linked list
has n elements. I.e. each vertex is a linked list of
adjacent vertices.
Example:
ABC
BC
C
DC
56
Suitability
Consider operation of finding if there is an edge between two
vertices v1 and v2.
In adjacency matrix:
Examine value of G[V1, V2].
In adjacency list:
Locate V1 in primary list, look for V2 in secondary list.
In adjacency list:
Go the V1 element.
The secondary list is the list of adjacent vertices.
Topological Sort
Major application is job scheduling.
Consider example from:
https://sys.cs.rice.edu/course/comp314/07/le
ctures/11-topological-sort/
58
Topological Sort
Consider a DAG (directed, acyclic).
A natural ordering.
Consider the graph:
A
B
D
B
C
E
C
D
E
Algorithm
Repeat:
Find a vertex with no successor.
Remove it from graph.
Put it at beginning of list.
60
61
Graph Traversals
Depth first:
Like depth first for a tree.
But a tree specifies a root and a graph does not. In
other words we have to specify a starting vertex.
Algorithm
-- Global
bool[] visited = new bool[number of vertices]
Set all visited to false.
DepthFirst(vertex v)
{
Visit v
visited[v.number] = true
for each successor s of v
{
if (visited[v.number] = false)
{
DepthFirst(s)
}
}
}
63
64
65