0% found this document useful (0 votes)
12 views58 pages

Wa001

The document provides an overview of graph theory concepts, including definitions of graphs, types of graphs (directed, undirected, weighted), and graph representations (adjacency matrix and list). It also discusses graph traversal algorithms such as Depth First and Breadth First Traversal, along with their implementations. Additionally, it covers the properties of connected and unconnected graphs, cycles, and the degree of vertices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views58 pages

Wa001

The document provides an overview of graph theory concepts, including definitions of graphs, types of graphs (directed, undirected, weighted), and graph representations (adjacency matrix and list). It also discusses graph traversal algorithms such as Depth First and Breadth First Traversal, along with their implementations. Additionally, it covers the properties of connected and unconnected graphs, cycles, and the degree of vertices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 58

Graph

Northwest Airline Flight

Anchorage
Boston
Minneapolis
Seattle
Hartford
SF

Austin Atlanta
Computer Network Or Internet
Charter Comcast

Regional Network

Intel UNT
Application
 Traveling Saleman

Start

 Find the shortest path that connects all


cities without a loop.
Concepts of Graphs

node or vertex

edges (weight)
Graph Definition
 A graph G = (V,E) is composed of:
V: set of vertices (nodes)
E: set of edges (arcs) connecting the vertices in
V
 An edge e = (u,v) is a pair of vertices
 Example: a b V= {a,b,c,d,e}

E= {(a,b),(a,c),
c (a,d),
(b,e),(c,d),(c,e),
(d,e)}
d e
Undirected vs. Directed Graph

Undirected Graph Directed Graph


– edge has no oriented – edge has oriented vertex
Degree of a Vertex
 The degree of a vertex is the number of edges
to that vertex
 For directed graph,
 the in-degree of a vertex v is the number of edges
that have v as the head
 the out-degree of a vertex v is the number of edges
that have v as the tail
 if di is the degree of a vertex i in a graph G with n
vertices and e edges, the number of edges is
n 1

e ( d )/ 2
0
i
Hint: Adjacent vertices are
counted twice.
Subgraph
 Subgraph:
 subset of vertices and edges
Simple Path
 A simple path is a path such that all vertices are distinct,
except that the first and the last could be the same.
 ABCD is a simple path

A C

path
D
Cycle
 A cycle is a path that starts and ends at the same point. For
undirected graph, the edges are distinct.
 CBDC is a cycle

A C

D
Connected vs. Unconnected
Graph

Connected Graph Unconnected Graph


Directed Acyclic Graph
 Directed Acyclic Graph (DAG) : directed
graph without cycle
Weighted Graph
 Weighted graph: a graph with numbers
assigned to its edges
 Weight: cost, distance, travel time, hop, etc.

0
20 10
1 1
2
4
5
3
Representation Of Graph
 Two representations

 Adjacency Matrix

 Adjacency List
Adjacency Matrix
 Assume N nodes in graph
 Use Matrix A[0…N-1][0…N-1]
 if vertex i and vertex j are adjacent in graph,
A[i][j] = 1,
 otherwise A[i][j] = 0
 if vertex i has a loop, A[i][i] = 1
 if vertex i has no loop, A[i][i] = 0
Example of Adjacency Matrix
A[i][j] 0 1 2 3
0 0 0 1 1 0
1 1 0 1 1
1 2 1 1 0 1
2
3 0 1 1 0
3 0 1 1 0
1 0 1 1
So, Matrix A = 1 1 0 1
0 1 1 0
Undirected vs. Directed
 Undirected graph
 adjacency matrix is symmetric
 A[i][j]=A[j][i]
 Directed graph
 adjacency matrix may not be symmetric
 A[i][j]A[j][i]
Directed Graph
A[i][j] 0 1 2 3
0 0 1 1 1
0
1 0 0 0 1
2 0 0 0 1
1
2 3 0 0 0 0

3 0 1 1 1
0 0 0 1
So, Matrix A = 0 0 0 1
0 0 0 0
Weighted Graph
A[i][j] 0 1 2 3
0 0 20 10 1
1 20 0 0 5
0 2 10 0 0 4
20 10
3 1 5 4 0
1 1
2
4 0 20 10 1
5 20 0 0 5
3
So, Matrix A = 10 0 0 4
1 5 4 0
Adjacency List
 An array of list
 the ith element of the array is a list of vertices that
connect to vertex i
0 1 2 3
0 1 3

1 2 3
2
3
3
vertex 0 connect to vertex 1, 2 and 3
vertex 1 connects to 3
vertex 2 connects to 3
Weighted Graph
 Weighted graph: extend each node with an
addition field: weight

0 0 1 10 2 20 3 1
20 10
1 0 10 3 4
1 1
2 2 0 20 3 5
4
5 3 0 1 1 4 2 5
3
Comparison Of Representations

Adjacency Adjacency
Cost
Matrix List
Given two vertices u and v: degree of
find out whether u and v are O(1) node
adjacent O(N)
degree of
Given a vertex u:
O(N) node
enumerate all neighbors of u
O(N)
Summations
For all vertices:
of all node
enumerate all neighbors of each O(N2) degree
vertex
O(E)
Complete Graph
• There is an edge between any two vertices

Total number of edges


in graph:
E = N(N-1)/2 = O(N2)
Sparse Graph
• There is a very small number of edges in the
graph
For example:
E = N-1= O(N)
Space Requirements
 Memory space:
 adjacency matrix O(N2)
 adjacency list O(E)
 Sparse graph
 adjacency list is better
 Dense graph
 same running time
Graph Traversal
 List out all cities that United Airline can
reach from Hartford Airport
CHI

Hartford
SF

NYC

LA
DC
Graph Traversal
 From vertex u, list out all vertices that can
be reached in graph G
 Set of nodes to expand
 Each node has a flag to indicate visited or
not
Traversal Algorithm
 Step 1: { Hartford }
 findneighbors of Hartford
 { Hartford, NYC, CHI }
CHI

SF Hartford

NYC

LA
W. DC
Traversal Algorithm
 Step 2: { Hartford, NYC, CHI }
 findneighbors of NYC, CHI
 { Hartford, NYC, CHI, LA, SF }

CHI

SF Hartford

NYC

LA
W. DC
Traversal Algorithm
 Step 3: {Hartford, NYC, CHI, LA, SF }
 findneighbors of LA, SF
 no other new neighbors
CHI

SF Hartford

NYC

LA
W. DC
Traversal Algorithm
 Finally we get all cities that United
Airline can reach from Hartford Airport
 {Hartford, NYC, CHI, LA, SF }
CHI

SF Hartford

NYC

LA
W. DC
Algorithm of Graph Traversal
1. Mark all nodes as unvisited
2. Pick a starting vertex u, add u to probing list
3. While ( probing list is not empty)
{
Remove a node v from probing list
Mark node v as visited
For each neighbor w of v, if w is unvisited,
add w to the probing list
}
Graph Traversal Algorithms
 Two algorithms
 Depth First Traversal
 Breadth First Traversal
Depth First Traversal
 Probing List is implemented as stack (LIFO)
 Example
 A’s neighbor: B, C, E A
 B’s neighbor: A, C, F
 C’s neighbor: A, B, D B
C E
 D’s neighbor: E, C, F
 E’s neighbor: A, D D
F
 F’s neighbor: B, D
 start from vertex A
Depth First Traversal (Cont)
– A’s neighbor: B C E
– B’s neighbor: A C F
A
– C’s neighbor: A B D
– D’s neighbor: E C F B
– C E
E’s neighbor: A D
– F’s neighbor: B D
F D
 Initial State
 Visited Vertices { }
 Probing Vertices { A }
 Unvisited Vertices { A, B, C, D, E, F }
stack A
Depth First Traversal (Cont)
– A’s neighbor: B C E
– B’s neighbor: A C F
A
– C’s neighbor: A B D
– D’s neighbor: E C F B
– C E
E’s neighbor: A D
– F’s neighbor: B D
F D
 Peek a vertex from stack, it is A, mark it
as visited
 Find A’s first unvisited neighbor, push it
into stack
 Visited Vertices { A }
 Probing vertices { A, B }
B
 Unvisited Vertices { B, C, D, E, F }
A A
stack
Depth First Traversal (Cont)
– A’s neighbor: B C E
– B’s neighbor: A C F
A
– C’s neighbor: A B D
– D’s neighbor: E C F B
– C E
E’s neighbor: A D
– F’s neighbor: B D
F D
 Peek a vertex from stack, it is B, mark
it as visited
 Find B’s first unvisited neighbor, push
it in stack
 Visited Vertices { A, B } C
 Probing Vertices { A, B, C }
B B
 Unvisited Vertices { C, D, E, F }
A A
stack
Depth First Traversal (Cont)
– A’s neighbor: B C E
– B’s neighbor: A C F
A
– C’s neighbor: A B D
– D’s neighbor: E C F B
– C E
E’s neighbor: A D
– F’s neighbor: B D
F D
 Peek a vertex from stack, it is C, mark it
as visited
 Find C’s first unvisited neighbor, push it
in stack D
 Visited Vertices { A, B, C } C C
 Probing Vertices { A, B, C, D }
B B
 Unvisited Vertices { D, E, F }
A A
stack
Depth First Traversal (Cont)
– A’s neighbor: B C E
– B’s neighbor: A C F
A
– C’s neighbor: A B D
– D’s neighbor: E C F B
– C E
E’s neighbor: A D
– F’s neighbor: B D
F D
 Peek a vertex from stack, it is D, mark
it as visited
 Find D’s first unvisited neighbor, push E
it in stack D D
 Visited Vertices { A, B, C, D } C C
 Probing Vertices { A, B, C, D, E }
B B
 Unvisited Vertices { E, F }
A A
stack
Depth First Traversal (Cont)
– A’s neighbor: B C E
– B’s neighbor: A C F
A
– C’s neighbor: A B D
– D’s neighbor: E C F B
– C E
E’s neighbor: A D
– F’s neighbor: B D
F D
 Peek a vertex from stack, it is E, mark it
as visited
 Find E’s first unvisited neighbor, no E
vertex found, Pop E D D
 Visited Vertices { A, B, C, D, E } C C
 Probing Vertices { A, B, C, D }
B B
 Unvisited Vertices { F }
A A
stack
Depth First Traversal (Cont)
– A’s neighbor: B C E
– B’s neighbor: A C F
A
– C’s neighbor: A B D
– D’s neighbor: E C F B
– C E
E’s neighbor: A D
– F’s neighbor: B D
F D
 Peek a vertex from stack, it is D, mark it
as visited
 Find D’s first unvisited neighbor, push it F
in stack D D
 Visited Vertices { A, B, C, D, E } C C
 Probing Vertices { A, B, C, D, F}
B B
 Unvisited Vertices { F }
A A
stack
Depth First Traversal (Cont)
– A’s neighbor: B C E
– B’s neighbor: A C F
A
– C’s neighbor: A B D
– D’s neighbor: E C F B
– C E
E’s neighbor: A D
– F’s neighbor: B D
F D
 Peek a vertex from stack, it is F, mark it
as visited
 Find F’s first unvisited neighbor, no F
vertex found, Pop F D D
 Visited Vertices { A, B, C, D, E, F } C C
 Probing Vertices { A, B, C, D}
B B
 Unvisited Vertices { }
A A
stack
Depth First Traversal (Cont)
– A’s neighbor: B C E
– B’s neighbor: A C F
A
– C’s neighbor: A B D
– D’s neighbor: E C F B
– C E
E’s neighbor: A D
– F’s neighbor: B D
F D
 Peek a vertex from stack, it is D, mark it
as visited
 Find D’s first unvisited neighbor, no
vertex found, Pop D D
 Visited Vertices { A, B, C, D, E, F } C C
 Probing Vertices { A, B, C }
B B
 Unvisited Vertices { }
A A
stack
Depth First Traversal (Cont)
– A’s neighbor: B C E
– B’s neighbor: A C F
A
– C’s neighbor: A B D
– D’s neighbor: E C F B
– C E
E’s neighbor: A D
– F’s neighbor: B D
F D
 Peek a vertex from stack, it is C, mark it
as visited
 Find C’s first unvisited neighbor, no
vertex found, Pop C
 Visited Vertices { A, B, C, D, E, F } C
 Probing Vertices { A, B }
B B
 Unvisited Vertices { }
A A
stack
Depth First Traversal (Cont)
– A’s neighbor: B C E
– B’s neighbor: A C F
A
– C’s neighbor: A B D
– D’s neighbor: E C F B
– C E
E’s neighbor: A D
– F’s neighbor: B D
F D
 Peek a vertex from stack, it is B, mark it
as visited
 Find B’s first unvisited neighbor, no
vertex found, Pop B
 Visited Vertices { A, B, C, D, E, F }
 Probing Vertices { A }
B
 Unvisited Vertices { }
A A
stack
Depth First Traversal (Cont)
– A’s neighbor: B C E
– B’s neighbor: A C F
A
– C’s neighbor: A B D
– D’s neighbor: E C F B
– C E
E’s neighbor: A D
– F’s neighbor: B D
F D
 Peek a vertex from stack, it is A, mark it
as visited
 Find A’s first unvisited neighbor, no
vertex found, Pop A
 Visited Vertices { A, B, C, D, E, F }
 Probing Vertices { }
 Unvisited Vertices { }
A
stack
Depth First Traversal (Cont)
– A’s neighbor: B C E
– B’s neighbor: A C F
A
– C’s neighbor: A B D
– D’s neighbor: E C F B
– C E
E’s neighbor: A D
– F’s neighbor: B D
F D
 Now probing list is empty
 End of Depth First Traversal
 VisitedVertices { A, B, C, D, E, F }
 Probing Vertices { }
 Unvisited Vertices { }

stack
Breadth First Traversal
 Probing List is implemented as queue
(FIFO)
 Example A
 A’s neighbor: B C E
 B’s neighbor: A C F B
C E
 C’s neighbor: A B D
 D’s neighbor: E C F D
F
 E’s neighbor: A D
 F’s neighbor: B D
 start from vertex A
Breadth First Traversal (Cont)
– A’s neighbor: B C E
– B’s neighbor: A C F
A
– C’s neighbor: A B D
– D’s neighbor: E C F B
– C E
E’s neighbor: A D
– F’s neighbor: B D
F D
 Initial State
 Visited Vertices { }
 Probing Vertices { A } A
 Unvisited Vertices { A, B, C, D, E, F } queue
Breadth First Traversal (Cont)
– A’s neighbor: B C E
– B’s neighbor: A C F
A
– C’s neighbor: A B D
– D’s neighbor: E C F B
– C E
E’s neighbor: A D
– F’s neighbor: B D
F D
 Delete first vertex from queue, it is A, mark
it as visited
 Find A’s all unvisited neighbors, mark them
as visited, put them into queue A
 Visited Vertices { A, B, C, E }
 Probing Vertices { B, C, E } B C E
 Unvisited Vertices { D, F }
queue
Breadth First Traversal (Cont)
– A’s neighbor: B C E
– B’s neighbor: A C F
A
– C’s neighbor: A B D
– D’s neighbor: E C F B
– C E
E’s neighbor: A D
– F’s neighbor: B D
F D
 Delete first vertex from queue, it is B, mark
it as visited
 Find B’s all unvisited neighbors, mark them
as visited, put them into queue B C E
 Visited Vertices { A, B, C, E, F }
 Probing Vertices { C, E, F } C E F
 Unvisited Vertices { D }
queue
Breadth First Traversal (Cont)
– A’s neighbor: B C E
– B’s neighbor: A C F
A
– C’s neighbor: A B D
– D’s neighbor: E C F B
– C E
E’s neighbor: A D
– F’s neighbor: B D
F D
 Delete first vertex from queue, it is C, mark
it as visited
 Find C’s all unvisited neighbors, mark
them as visited, put them into queue C E F
 Visited Vertices { A, B, C, E, F, D }
 Probing Vertices { E, F, D } E F D
 Unvisited Vertices { }
queue
Breadth First Traversal (Cont)
– A’s neighbor: B C E
– B’s neighbor: A C F
A
– C’s neighbor: A B D
– D’s neighbor: E C F B
– C E
E’s neighbor: A D
– F’s neighbor: B D
F D
 Delete first vertex from queue, it is E, mark
it as visited
 Find E’s all unvisited neighbors, no vertex
found E F D
 Visited Vertices { A, B, C, E, F, D }
 Probing Vertices { F, D } F D
 Unvisited Vertices { }
queue
Breadth First Traversal (Cont)
– A’s neighbor: B C E
– B’s neighbor: A C F
A
– C’s neighbor: A B D
– D’s neighbor: E C F B
– C E
E’s neighbor: A D
– F’s neighbor: B D
F D
 Delete first vertex from queue, it is F, mark
it as visited
 Find F’s all unvisited neighbors, no vertex
found F D
 Visited Vertices { A, B, C, E, F, D }
 Probing Vertices { D } D
 Unvisited Vertices { }
queue
Breadth First Traversal (Cont)
– A’s neighbor: B C E
– B’s neighbor: A C F
A
– C’s neighbor: A B D
– D’s neighbor: E C F B
– C E
E’s neighbor: A D
– F’s neighbor: B D
F D
 Delete first vertex from queue, it is D, mark
it as visited
 Find D’s all unvisited neighbors, no vertex
found D
 Visited Vertices { A, B, C, E, F, D }
 Probing Vertices { }
 Unvisited Vertices { }
queue
Breadth First Traversal (Cont)
– A’s neighbor: B C E
– B’s neighbor: A C F
A
– C’s neighbor: A B D
– D’s neighbor: E C F B
– C E
E’s neighbor: A D
– F’s neighbor: B D
F D
 Now the queue is empty
 End of Breadth First Traversal
 Visited Vertices { A, B, C, E, F, D }
 Probing Vertices { }
 Unvisited Vertices { }

queue
Difference Between DFT & BFT
 Depth First Traversal (DFT)
 order of visited: A, B, C, D, E, F A

B E
C
 Breadth First Traversal (BFT)
 order F D
of visited: A, B, C, E, F, D

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy