You are on page 1of 31

Graphs

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}

 A pseudograph is a multigraph with the condition


vi≠vj removed
Definitions…
A path from v1to vn is a sequence of edges edge(v1 v2),
edge(v2 v3), ... ,edge(vn–1 vn) and is denoted as path
v1, v2, v3 , ... , vn–1, vn
If v1= vn and no edge is repeated, then the path is called a
circuit

If all vertices in a circuit are different, then it is


Called a cycle
Definitions…
 A graph is called a weighted graph if each edge has an
assigned number.
 The number assigned to an edge is can be weight, cost,
distance, length, or any other thing.
Definitions…
A graph with n vertices is called complete and is denoted Kn
if for each pair of distinct vertices there is exactly one edge
connecting them;

The number of edges in such a graph |E|=

The degree of a vertex v, deg(v), is the number of edges


incident with v.
Graph Representation
 Two vertices vi and vj are called adjacent if the edge(vivj) is
in E
 A simple representation is given by an adjacency list which
specifies all vertices adjacent to each vertex of the graph
1. This list can be implemented as a table(star representation)
Graph Representation
a. adjacency matrix |V|×|V|

For multigraphs aij= number of edges between vi and vj

b. incidence matrix |V|×|E|


Graph Representation
2. Linked List implementation

Which one is best? Depends on problems


 process vertices adjacent to a vertex v

List- deg(v)
Matrix- |V|steps
 Insertion and deletion of vertex v

List- need linked list maintenance


Matrix-changing 0 to 1
Graph Traversal
 Need mark to avoid revisiting
depth-first search algorithm

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

 Step 2: If possible, visit an adjacent unvisited vertex, mark


it, and push it on the stack.
 Step 3: If there are no unvisited nodes, then pop a vertex
off the stack, if it is not empty, and repeat step 2
 Step 4: If stack is empty, you are done
Graph Traversal…
Complexity O(|V|+ |E|)
Application
Shortest Path
 the shortest weighted path from v1 to v6 has a cost of 6

 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!!!

You might also like