DataStructure - Graphs

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 24

Lecture outlines

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

Networks (roads, flights, communications)


JFK LAX LAX STL

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

Intuition Behind Graphs


The nodes represent entities (such as people, cities, computers, words, etc.) Edges (x,y) represent relationships between entities x and y, such as:
x is a friend of y (note that this not necessarily reciprocal) x is a child of y

Definition and Terminology


What is a graph? A graph G = (V,E) is composed of: V: set of vertices E: set of edges 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),(a,d), (b,e),(c,d),(c,e), (d,e)}

a c

V={a, b, c} E={<a, b>,<a, c>, <c,b>}

d
7

Definition and Terminology


Graph may be either directed or undirected
Directed graph (or Digraph)
Each line has a direction to its successor. The lines in a directed graph are known as arcs Note <vi, vj> <vj, vi>

Undirected graph
Each line has no direction. Note <vi, vj> = <vj, vi>

Definition and Terminology


0
1 2 1 3 G1
complete graph V(G1)={0,1,2,3} V(G2)={0,1,2,3,4,5,6} V(G3)={0,1,2}

0 2 4 G2

0 1 6
incomplete graph

3 5

2 G3

E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)} E(G3)={<0,1>,<1,0>,<1,2>}

A graph is said to b a complete graph if there is an edge between every pair of vertices.
9

Loop edge and multiple edges


0

1
self edge (a)

(b)

multigraph: multiple occurrences of the same edge

10

Definition and Terminology


Path: sequence of vertices v1,v2,. . .vk such that consecutive vertices vi and vi+1 are adjacent {A, B, C, E} ? {E,C,B,A}? {A,B,C,B,E}?

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

Definition and Terminology


Weighted graph:
When a no. or weight is associate with every edge, is called weighted graph. Weight are some times called cost.

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

Definition and Terminology


Connectivity
Connected undirected graph: any two vertices are connected by some path in undirected graph

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

Definition and Terminology


Degree in undirected graph
The degree of a vertex is the number of edges incident to that vertex Example:

The degrees of the nodes A, C, D, F = 1


15

The degrees of the nodes B, E = 3

Definition and Terminology


Degree in directed graph

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

Max and Min Degree of the Graph


Max Degree:
The highest degree of the node in the graph is called the Max Degree on the Graph

Min Degree:
The minimum degree of the node in the graph is called the Min Degree on the Graph

17

Graph Traversal Techniques


There are two standard graph traversal techniques:
Depth-First Search (DFS) Breadth-First Search (BFS)

18

Graph Traversal (Contd.)


In both DFS and BFS, the nodes of the undirected graph are visited in a systematic manner so that every node is visited exactly one. Both BFS and DFS give rise to a tree:
When a node x is visited, it is labeled as visited, and it is added to the tree If the traversal got to node x from node y, y is viewed as the parent of x, and x a child of y
19

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

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