Professional Documents
Culture Documents
CHAPTER 6 LEC 1
Outline
Definitions
Graph Representation
Graph Traversal
Motivation
Trees are limited to represent hirarchical relationship
Graph is a collection of vertices (or Node) and a
connection between them. No restriction on the number of
vertices and connection between them.
Definitions
A simple graph G= (V,E) consists of a nonempty set V
of vertices and a possibly empty set E of edges, each edge
being a set of two vertices from V.[fig a,b](v i ,v j)=(vj,v i)
A directed graph or a digraph, G= (V,E) consists of a
nonempty set V of vertices and a set E of edges (also
called arcs),where each edge is a pair of vertices from V.
[fig g] => ( v i , v j ) ≠ (v j , v i)
Definitions…
A multigraph is a graph in which two vertices can be
joined by multiple edges.
G= (V,E,f ) f:E→{{vi,vj} : vi, vj ∈V and vi ≠ vj}
List- deg(v)
Matrix- |V|steps
Insertion and deletion of vertex v
DFS(v) depthFirstSearch()
num(v)= i++; for all vertices v
for all vertices u adjacent to v num(v) = 0;
if num(u) is 0 edges = null;
attach edge(uv) to edges; i = 1;
DFS(u); while there is a vertex v such that num(v) is 0
DFS(v);
output edges;
Graph Traversal…
DFS uses a stack to remember where it should go when it
reaches a dead end.
• Steps in DFS:
Step 1: Pick a starting node
negative-cost cycle
Unweighted Shortest Paths
Only interested in the number of edges
Unweighted Shortest Paths
Unweighted Shortest Paths
Initial configuration of
the table
Dijkstra’s Algorithm
Dijkstra’s Algorithm
After v1is declared known After v4is declared known
Dijkstra’s Algorithm
After V2 is declared known After v5 and then v3 are declared known
Dijkstra’s Algorithm
After v7 is declared known After v6 is declared known and
algorithm terminates
Dijkstra’s Algorithm – pseudo code
Dijkstra’s - Performance
Using a simple implementation:
• Finding next node with minimum distance will take 𝑂(𝑉)
• A total of 𝑂(|𝑉|2) over the course of the algorithm
Distance has to be updated once for edge
• this takes 𝑂 |𝐸| over the course of the algorithm
• Total: 𝑂 (|𝐸 | + |𝑉|2)
See u next class!!!