Академический Документы
Профессиональный Документы
Культура Документы
• All tree structures are hierarchical. This means that each node
can only have one parent node(only parent to child path).
Trees can be used to store data which has a definite hierarchy;
for example a family tree or a computer file system.
{a,b} {a,c}
{b,d} {c,d}
{b,e} {c,f}
{e,f}
Terminology
• If v1 and v2 are connected, they are said to be adjacent
vertices
• v1 and v2 are endpoints of the edge {v1, v2}
*Later, we will talk about “directed graphs”, where edges have direction. This
means that {v1,v2} ≠ {v2,v1} . Directed graphs are drawn with arrows (called arcs)
between edges. A B This means {A,B} only, not {B,A}
Path between vertices
• A path is a sequence of vertices (v0, v1, v2,…
vk) such that:
Note: a path is allowed to go through the same vertex or the same edge any
number of times!
D C
A E
1. Adjacency Matrix
Use a 2D matrix to represent the graph
2. Adjacency List
Use a 1D array of linked lists
Adjacency Matrix
0 1 2 3 4 5 6 7 8 9
0
0 0 0 0 0 0 0 0 0 1 0
8
1 0 0 1 1 0 0 0 1 0 1
2 0 1 0 0 1 0 0 0 1 0
2 9
3 0 1 0 0 1 1 0 0 0 0
1
4 0 0 1 1 0 0 0 0 0 0
3 7
5 0 0 0 1 0 0 1 0 0 0
6
4 6 0 0 0 0 0 1 0 1 0 0
5
7 0 1 0 0 0 0 1 0 0 0
8 1 0 1 0 0 0 0 0 0 1
9 0 1 0 0 0 0 0 0 1 0
Examples
0 8
0
1 2 3 7 9
8
2 1 4 8
2 3 1 4 5
9
1 4 2 3
5 3 6
3 7
6 5 7
6
4 7 1 6
5
8 0 2 9
9 1 8
Algorithm:-
Step-1: Make the list that holds the graph, having all the
vertices in the graph
Step-2: For every vertex in that list do the following -
a. find the adjacent vertices, if any
b. add that vertex in the adjacency list for this
current vertex
struct adj_node /*the NODE which forms
the part of the adjacency
list; look at figure [B]*/
{
char vertex_val;
struct adj_node *adj_next; /*red pointer*/
};
struct node /*the NODE which forms
the part of the Graph; look
at figure [A]*/
{
char vertex_val;
struct adj_node *down;
struct node *next; /*black pointer*/
};
struct node *graph;
LinkListrepresentation()
{
struct node *cur_vertex, *aux_ptr;
struct adj_node *adj_cur, *new;
Implementation:
• Head[]( array of pointers) is set to NULL in main().
• Creategraph() is a function to create graph(array of pointers and
linked list).
• Creategraph() asks the user for number of nodes in graph and builds
the list by asking user to enter the adjacent vertices for node.
• For each adjacent vertex a node is created and inserted at rear.
//creating a graph using array of pointers and linked list
void Creategraph(NODEPTR head[])
{
int i,v;
printf(“enter no of vertices”);
scanf(“%d”,&n);
for(i=0;i<n;i++){
do{
printf(“enter next adjacent vertex for %d”, i);
scanf(“%d”, &v);
if(v==-1) break; // no more adjacent vertex
head[i]=insert_rear(head[i],v);
} while(1);
}// end for
}end function
//insert rear function
NODEPTR insert_rear(NODEPTR h, int v)
{
NODEPTR temp=getnode();
NODEPTR t=h;
tempvertex_value=v;
tempnext=NULL;
if(h==NULL)
return temp;
while(tnext!=NULL)
t=tnext;
tnext=temp;
return h;
}
//function for displaying graph
Void displaygraph(NODEPTR head[ ])
{
NODEPTR t;
for(int i=0;i<n;i++)
{
cout<<i;
t=h[i];
while(t!=NULL)
{
cout<<tvertex_value;
t=tnext;
}
}
}
//main function
Struct node
{
int vertex_value;
struct node *next;
};
Int n;
Void main()
{
NODEPTR head[20]={NULL};
creategraph(head);
displaygraph(head);
}
Representations
Adjacency Lists vs. Matrix
• Adjacency Lists
– More compact than adjacency matrices if graph has few edges
– Requires more time to find if an edge exists
• Adjacency Matrix
– Always require n2 space
• This can waste a lot of space if the number of edges are
sparse
– Can quickly find if an edge exists
Directed Acyclic Graph
• A directed path is a sequence of vertices
(v0, v1, . . . , vk)
– Such that (vi, vi+1) is an arc
• A directed cycle is a directed path such that the first and last
vertices are the same.
Outdeg(0)?
0 7
2 9
1 Num of Edges?
Total OutDeg?
5 Total Indeg?
4
Directed Graphs Usage
• Directed graphs are often used to represent order-dependent
tasks
• That is we cannot start a task before another task finishes
• We can model this task dependent constraint using arcs
• An arc (i,j) means task j cannot start until task i is finished
i j
• Depth-First Traversals.
– Algorithms.
– Example.
– Implementation.
• Breadth-First Traversal.
– The Algorithm.
– Example.
– Implementation.
• Review Questions.
Depth-First Traversal Algorithm
• In this method, After visiting a vertex v, which is adjacent to w1, w2, w3, ...;
Next we visit one of v's adjacent vertices, w1 say. Next, we visit all vertices
adjacent to w1 before coming back to w2, etc.
Algorithm:-
Step-1: Set the starting point of traversal and push it inside the stack.
Step-2: Pop the stack and add the popped vertex to the list of visited
vertices.
Step-3: Find the adjacent vertices for the most recently visited
vertex( from the Adjacency Matrix).
Step-4: Push these adjacent vertices inside the stack if they are not
visited and not there in the stack already.
Step-5: Go to step-2 if the stack is not empty.
• Note: Adjacent vertices can be pushed in any order; but to obtain a unique traversal,
we will push them in reverse alphabetical order.
Example
• Demonstrates depth-first traversal using an explicit stack.
A B C F E G D H I Stack
//depth first search
Void dfs( NODEPTR h[], int v)
{
NODEPTR w;
int visit[20]={FALSE};
cout<<v;
visit[v]=TRUE; //mark node as visited
for(w=h[v] ;w!=NULL; w=wnext){
if(!visit[wvertex_value]){
dfs(h,wvertex_value);
}
}
}
Breadth-First Traversal Algorithm
• In this method, After visiting a vertex v, we must visit all its adjacent
vertices w1, w2, w3, ..., before going down next level to visit vertices
adjacent to w1 etc.
• Note: Adjacent vertices can be enqueued in any order; but to obtain a unique
traversal, we will enqueue them in alphabetical order.
BSF algorithm
Queue front
Order of
Traversal A B D E C G F H IQueue rear
Example
Adjacency List Visited Table (T/F)
0 F
1 F
0
2 F
8 3 F
4 F
source 2 9 5 F
6
1 F
7 F
3 7 8 F
6 9 F
4
5
Initialize visited
table (all False)
Q= { }
Initialize Q to be empty
Example
Adjacency List Visited Table (T/F)
0 F
1 F
0
2 T
8 3 F
4 F
source 2 9 5 F
6
1 F
7 F
3 7 8 F
6 9 F
4
5
Flag that 2 has
been visited.
Q= { 2 }
1 T
0
2 T
Neighbors
8 3 F
4 T
source 2 9 5 F
6
1 F
7 F
3 7 8 T
6 9 F
4
5
Mark neighbors
as visited.
Q = {2} → { 8, 1, 4 }
Dequeue 2.
Place all unvisited neighbors of 2 on the queue
Example
Adjacency List Visited Table (T/F)
0 T
1 T
0
2 T
8 3 F
4 T
source 2 9 5 F
6
1 F
7 F
3 7 8 T
Neighbors
6 9 T
4
5
Mark new visited
Neighbors.
Q = { 8, 1, 4 } → { 1, 4, 0, 9 }
Dequeue 8.
-- Place all unvisited neighbors of 8 on the queue.
-- Notice that 2 is not placed on the queue again, it has been visited!
Example
Adjacency List Visited Table (T/F)
0 T
1 T
0 Neighbors
2 T
8 3 T
4 T
source 2 9 5 F
6
1 F
7 T
3 7 8 T
6 9 T
4
5
Mark new visited
Neighbors.
Q = { 1, 4, 0, 9 } → { 4, 0, 9, 3, 7 }
Dequeue 1.
-- Place all unvisited neighbors of 1 on the queue.
-- Only nodes 3 and 7 haven’t been visited yet.
Example
Adjacency List Visited Table (T/F)
0 T
1 T
0
2 T
8 3 T
4 T
Neighbors
source 2 9 5 F
6
1 F
7 T
3 7 8 T
6 9 T
4
5
Q = { 4, 0, 9, 3, 7 } → { 0, 9, 3, 7 }
Dequeue 4.
-- 4 has no unvisited neighbors!
Example
Adjacency List Visited Table (T/F)
0 T
Neighbors
1 T
0
2 T
8 3 T
4 T
source 2 9 5 F
6
1 F
7 T
3 7 8 T
6 9 T
4
5
Q = { 0, 9, 3, 7 } → { 9, 3, 7 }
Dequeue 0.
-- 0 has no unvisited neighbors!
Example
Adjacency List Visited Table (T/F)
0 T
1 T
0
2 T
8 3 T
4 T
source 2 9 5 F
6
1 F
7 T
3 7 8 T
6 Neighbors 9 T
4
5
Q = { 9, 3, 7 } → { 3, 7 }
Dequeue 9.
-- 9 has no unvisited neighbors!
Example
Adjacency List Visited Table (T/F)
0 T
1 T
0
2 T
8 3 T
Neighbors
4 T
source 2 9 5 T
6
1 F
7 T
3 7 8 T
6 9 T
4
5
Mark new visited
Vertex 5.
Q = { 3, 7 } → { 7, 5 }
Dequeue 3.
-- place neighbor 5 on the queue.
Example
Adjacency List Visited Table (T/F)
0 T
1 T
0
2 T
8 3 T
4 T
source 2 9 5 T
6
1 T
Neighbors 7 T
3 7 8 T
6 9 T
4
5
Mark new visited
Vertex 6.
Q = { 7, 5 } → { 5, 6 }
Dequeue 7.
-- place neighbor 6 on the queue.
Example
Adjacency List Visited Table (T/F)
0 T
1 T
0
2 T
8 3 T
4 T
source 2 9 Neighbors
5 T
6
1 T
7 T
3 7 8 T
6 9 T
4
5
Q = { 5, 6} → { 6 }
Dequeue 5.
-- no unvisited neighbors of 5.
Example
Adjacency List Visited Table (T/F)
0 T
1 T
0
2 T
8 3 T
4 T
source 2 9 5 T
1 Neighbors 6 T
7 T
3 7
8 T
6
4 9 T
5
Q= {6}→{ }
Dequeue 6.
-- no unvisited neighbors of 6.
Example
Adjacency List Visited Table (T/F)
0 T
1 T
0
2 T
8 3 T
4 T
source 2 9 5 T
6
1 T
7 T
3 7 8 T
6 9 T
4
5
1 F -
0
2 F -
8 3 F -
4 F -
source 2 9 5 F -
6
1 F -
7 F -
3 7 8 F -
6 9 F -
4
5 Pred
Initialize visited
table (all False)
Q= { } Initialize Pred to -1
Initialize Q to be empty
Example
Adjacency List Visited Table (T/F)
0 F -
1 F -
0
2 T -
8 3 F -
4 F -
source 2 9 5 F -
6
1 F -
7 F -
3 7 8 F -
6 9 F -
4
5 Pred
Flag that 2 has
been visited.
Q= { 2 }
1 T 2
0
2 T -
Neighbors
8 3 F -
4 T 2
source 2 9 5 F -
6 -
1 F
7 F -
3 7 8 T 2
6 9 F -
4
5 Pred
Mark neighbors
as visited.
1 T 2
0
2 T -
8 3 F -
4 T 2
source 2 9 5 F -
6
1 F -
7 F -
3 7 Neighbors 8 T 2
6 9 T 8
4
5 Pred
Mark new visited
Neighbors.
Q = { 8, 1, 4 } → { 1, 4, 0, 9 } Record in Pred
that we came
Dequeue 8.
from 8.
-- Place all unvisited neighbors of 8 on the queue.
-- Notice that 2 is not placed on the queue again, it has been visited!
Example
Adjacency List Visited Table (T/F)
0 T 8
1 T 2
0 Neighbors
2 T -
8 3 T 1
4 T 2
source 2 9 5 F -
6 -
1 F
7 T 1
3 7 8 T 2
6 9 T 8
4
5 Pred
Mark new visited
Neighbors.
Q = { 1, 4, 0, 9 } → { 4, 0, 9, 3, 7 } Record in Pred
that we came
Dequeue 1.
from 1.
-- Place all unvisited neighbors of 1 on the queue.
-- Only nodes 3 and 7 haven’t been visited yet.
Example
Adjacency List Visited Table (T/F)
0 T 8
1 T 2
0
2 T -
8 3 T 1
4 T 2
Neighbors
source 2 9 5 F -
6
1 F -
7 T 1
3 7 8 T 2
6 9 T 8
4
5 Pred
Q = { 4, 0, 9, 3, 7 } → { 0, 9, 3, 7 }
Dequeue 4.
-- 4 has no unvisited neighbors!
Example
Adjacency List Visited Table (T/F)
0 T 8
Neighbors
1 T 2
0
2 T -
8 3 T 1
4 T 2
source 2 9 5 F -
6
1 F -
7 T 1
3 7 8 T 2
6 9 T 8
4
5 Pred
Q = { 0, 9, 3, 7 } → { 9, 3, 7 }
Dequeue 0.
-- 0 has no unvisited neighbors!
Example
Adjacency List Visited Table (T/F)
0 T 8
1 T 2
0
2 T -
8 3 T 1
4 T 2
source 2 9 5 F -
6
1 F -
7 T 1
3 7 8 T 2
6 Neighbors 9 T 8
4
5 Pred
Q = { 9, 3, 7 } → { 3, 7 }
Dequeue 9.
-- 9 has no unvisited neighbors!
Example
Adjacency List Visited Table (T/F)
0 T
8
1 T
0 2
2 T
-
8 3 T
Neighbors 1
4 T
2
source 2 9 5 T
3
6
1 F
-
7 T
1
3 7 8 T
2
6 9 T
4 8
5
Pred
Mark new visited
Vertex 5.
Q = { 3, 7 } → { 7, 5 }
Record in Pred
Dequeue 3. that we came
-- place neighbor 5 on the queue. from 3.
Example
Adjacency List Visited Table (T/F)
0 T 8
1 T 2
0
2 T -
8 3 T 1
4 T 2
source 2 9 5 T 3
6
1 T 7
Neighbors 7 T 1
3 7 8 T 2
6 9 T 8
4
5 Pred
Mark new visited
Vertex 6.
Q = { 7, 5 } → { 5, 6 }
Record in Pred
Dequeue 7. that we came
-- place neighbor 6 on the queue. from 7.
Example
Adjacency List Visited Table (T/F)
0 T 8
1 T 2
0
2 T -
8 3 T 1
4 T 2
source 2 9 Neighbors
5 T 3
6
1 T 7
7 T 1
3 7 8 T 2
6 9 T 8
4
5 Pred
Q = { 5, 6} → { 6 }
Dequeue 5.
-- no unvisited neighbors of 5.
Example
Adjacency List Visited Table (T/F)
0 T 8
1 T 2
0
2 T -
8 3 T 1
4 T 2
source 2 9 5 T 3
6
1 Neighbors T 7
7 T 1
3 7 8 T 2
6 9 T 8
4
5 Pred
Q= {6}→{ }
Dequeue 6.
-- no unvisited neighbors of 6.
Example
Adjacency List Visited Table (T/F)
0 T 8
1 T 2
0
2 T -
8 3 T 1
4 T 2
source 2 9 5 T 3
6
1 T 7
7 T 1
3 7 8 T 2
6 9 T 8
4
5 Pred
1 2
2 -
3 1
4 2
5 3
6 7
7 1
8 2
9 8