DataStructure - Graphs
DataStructure - Graphs
DataStructure - Graphs
Definition of Graphs and Related Concepts Representation of Graphs Graph Applications Graph Traversal
Definition of Graphs
Graphs are non liner data structures. A graph is a finite set of nodes with line between nodes Nodes are called vertices or points. Lines are called edges or arcs. Formally, a graph G is a structure (V,E) consisting of
a finite set V called the set of nodes, and a finite set E called the set of edges/links , of the form (x,y) where x and y are nodes in V
2
Application of Graphs
Graph application area:
Computer science Electrical engineering Chemistry Data base Logic and design
computer networks airline flights road map course prerequisite structure tasks for completing a job flow of control through a program CS 103 many more
Graphs Applications
Electronic circuits CS16
DFW FTL
4
Examples of Graphs
V={0,1,2,3,4} E={(0,1), (1,2), (0,3), (3,0), (2,2), (4,3)}
0 1
2
When (x,y) is an edge, we say that x is adjacent to y, and y is adjacent from x. 0 is adjacent to 1. 1 is not adjacent to 0. 2 is adjacent from 1.
5
4
3
a c
d
7
Undirected graph
Each line has no direction. Note <vi, vj> = <vj, vi>
0 2 4 G2
0 1 6
incomplete graph
3 5
2 G3
A graph is said to b a complete graph if there is an edge between every pair of vertices.
9
1
self edge (a)
(b)
10
simple path: no repeated vertices, except possibly the first and the last {A, B, C, E} ? {E,C,B,A}?{A,B,C,B,E}? Cycle path: simple path, except that the last vertex is the same as the first vertex {B,C,D,E,B}?
11
Source nodes:
The node that have a positive out degree but zero in degree is called source node.
Sink nodes:
The node that has zero out degree but a positive in degree.
12
Weighted Graphs
Each edge e has a weight, wt(e) graph may be undirected or directed
weight may represent length, cost, capacity, etc adjacency matrix becomes weight matrix adjacency lists include weight in node
4 v
5 7 5 8
w 5
7 4 y
6 x
z a weighted graph G
13/24
Directed graph
connected not connected Strongly connected: A directed graph is strongly connected if there is a path from each vertex to every other vertex in the digraph. Weakly connected: A directed graph is weakly connected if at most two vertices are not connected. Disjoint: A graph is disjoint if it is not connected.
14
The degree of a vertex is the sum of the indegree and outdegree of lines incident to it.
The outdegree of a vertex in a digraph is the number of arcs leaving the vertex. The indegree is the number of arcs entering the vertex. Example: B&E
16
Min Degree:
The minimum degree of the node in the graph is called the Min Degree on the Graph
17
18
Graph searches
A B D H L M N O E I P F J C G K Q A tree search starts at the root and explores nodes from there, looking for a goal node (a node that satisfies certain conditions, depending on the problem) For some problems, any goal node is acceptable (N or J); for other problems, you want a minimum-depth goal node, that is, a goal node nearest the root (only J) Goal nodes
Depth-first searching
A B D H L M N O E I P F J C G K Q A depth-first search (DFS) explores a path all the way to a leaf before backtracking and exploring another path For example, after searching A, then B, then D, the search backtracks and tries another path from B Node are explored in the order
ABDEHLMNIOPCFGJ KQ
N will be found before J
Depth-First Search
DFS follows the following rules:
1. Select an unvisited node x, visit it, and treat as the current node 2. Find an unvisited neighbor of the current node, visit it, and make it the new current node; 3. If the current node has no unvisited neighbors, backtrack to the its parent, and make that parent the new current node; 4. Repeat steps 3 and 4 until no more nodes can be visited. 5. If there are still unvisited nodes, repeat from step 1.
22
Breadth-first searching
A B D H L M N O E I P F J C G K Q A breadth-first search (BFS) explores nodes nearest the root before exploring nodes further away For example, after searching A, then B, then C, the search proceeds with D, E, F, G Node are explored in the order
ABCDEFGHIJKLMNO PQ J will be found before N
Breadth-First Search
BFS follows the following rules:
1. Select an unvisited node x, visit it, make it the root in a BFS tree being formed. Its level is called the current level. 2. From each node z in the current level, in the order in which the level nodes were visited, visit all the unvisited neighbors of z. The newly visited nodes from this level form a new level that becomes the next current level. 3. Repeat step 2 until no more nodes can be visited. 4. If there are still unvisited nodes, repeat from Step 1.
24