Download presentation
Presentation is loading. Please wait.
Published byAgatha Patterson Modified over 8 years ago
1
Chapter 05 Introduction to Graph And Search Algorithms
2
2 A Graph is a data structure which consists of a set of vertices, and a set of edges that connect (some of) them. That is, G = ( V, E ), Where V - set of vertices, E - set of edges The edges indicate how we can move through the graph 1 2 3 4 5 Vertex (Node) Edge V = {1, 2, 3, 4, 5} E = { (1,2), (1,3), (1,4), (2,3), (3,5), (4,5) } What is Graph?
3
3 Computer Networks Electrical Circuits Road Map Computer Resistor/Inductor/… City Applications
4
4 A Directed Graph or Digraph is a graph where each edge has a direction The edges in a digraph are called Arcs or Directed Edges A directed graph edges’ allow travel in one direction Example: G = (V, E), where V = {1, 2, 3, 4, 5, 6} and E = {(1,4), (2,1), (2,3), (3,2), (4,3), (4,5), (4,6), (5,3), (6,1), (6,5)} (1, 4) = 1→4 where 1 is the tail and 4 is the head 1 4 3 6 5 2 Graph Categorization
5
5 An Undirected Graph is a graph where the edges have no directions The edges in an undirected graph are called Undirected Edges An undirected graph edges’ allow travel in either direction Example: G = (V, E), where V = {1, 2, 3, 4, 5} and E = {(1,2), (1,3), (1,4), (2,3), (3,5), (4,5)} 1 2 3 4 5 Graph Categorization (Contd.)
6
1 2 3 4 5 40 70 60 20 50 10 A Weighted Graph is a graph where all the edges are assigned weights. If the same pair of vertices have more than one edge, that graph is called a Multigraph Edges with the same end vertices are called parallel edges 1 2 3 Graph Categorization (Contd.) 6
7
7 Adjacent vertices: :two vertices are adjacent if they are connected by an edge An edge (i,j) is Incident to vertices i and j. Vertices 2 and 5 are not adjacent Loop or self edges: An edge ( i,i ) is called a self edge or a loop. In graphs loops are not permitted ( 1,1 ) and ( 4,4 ) are self edges 1 2 3 4 5 1 2 3 4 5 Graph Terminology
8
8 Adjacent Edges: two non parallel edges are adjacent if they have vertex in common An edge (i,j) is Incident to vertices i and j. 1 2 3 4 5 Graph Terminology
9
9 Path: A sequence of edges in the graph. A path is a subset of E that is a series of edges between two vertices There can be more than one path between two vertices. Vertex A is reachable from B if there is a path from A to B. Paths from B to D B, A, D B, C, D shortest path is the minimum number edges needed to connect two vertices Simple Path: A path where all the vertices are distinct.( without loops and parallel edges) 1,4,5,3 is a simple path. But 1,4,5,4 is not a simple path. A D C F E B G 1 2 3 4 5 Graph Terminology (Contd.)
10
10 Length : Sum of the lengths of the edges on the path. The length of a path in a graph is the number of edges in the path Length of the path 1,4,5,3 is 3 Circuit: A path whose first and last vertices are the same. The path 3,2,1,4,5,3 is a circuit. Cycle: A circuit where all the vertices are distinct except for the first (and the last) vertex. 1,4,5,3,1 is a cycle, but 1,4,5,4,1 is not a cycle. Hamiltonian Cycle: A Cycle that contains all the vertices of the graph. 1,4,5,3,2,1 is a Hamiltonian Cycle. 1 2 3 4 5 Graph Terminology (Contd.)
11
11 Degree of a Vertex : In an undirected graph, the no. of edges incident to the vertex In-degree: The no. of edges entering the vertex in a digraph Out-Degree: The no. of edges leaving the vertex in a digraph In-degree of 1 is 3 Out-degree of 1 is 1 A Subgraph of graph G=(V,E) is a graph H=(U,F) such that U Є V and F Є E 1 2 3 4 5 1 2 3 4 5 G=(V,E) 1 3 2 H=(U,F) Graph Terminology (Contd.)
12
12 A graph is said to be Connected if there is at least one path from every vertex to every other vertex in the graph. Tree: A connected undirected graph that contains no cycles Forest: A graph that does not contain a cycle 1 2 3 4 5 Connected 1 2 3 4 5 Unconnected 1 2 3 4 5 Tree 1 2 3 4 5 Forest Graph Terminology (Contd.)
13
13 The Spanning Tree of a Graph G is a subgraph of G that is a tree and contains all the vertices of G. 1 2 3 4 5 Graph 1 2 3 4 5 Spanning Tree Graph Terminology (Contd.)
14
14 Adjacency Matrix (A) The Adjacency Matrix A=(a i,j ) of a graph G=(V,E) with n nodes is an nXn matrix Each element of A is either 0 or 1, depending on the adjacency of the nodes a ij = 1, if (i,j) Є E, = 0, otherwise Example: Find the adjacency matrices of the following graphs. 1 2 3 4 5 01110 10100 11001 10001 00110 1 2 3 010 001 110 Representation of Graphs
15
15 Adjacency Matrix of a Weighted Graph The weight of the edge can be shown in the matrix when the vertices are adjacent A nil value (0 or ∞) depending on the problem is used when they are not adjacent Example: To find the minimum distance between nodes... ∞4∞ ∞∞5 95∞ 1 2 3 9 4 5 Representation of Graphs (Contd.)
16
16 Adjacency List An Adjacency list is an array of lists, each list showing the vertices a given vertex is adjacent to…. Adjacency List of a Weighted Graph The weight is included in the list 1 2 3 4 5 1 2 3 4 5 2 13 15 15 1 2 3 9 4 5 1 2 3 2 4 3 5 1 9 2 5 Representation of Graphs (Contd.)
17
Searching Graphs 17
18
Why do we need to search graphs? To find paths To look for connectivity Depth-First Search (DFS) Start from an arbitrary node Visit (Explore) an unvisited adjacent edge If the node visited is a dead end, go back to the previous node (Backtrack) Stop when no unvisited nodes are found and no backtracking can be done Implemented using a Stack Explore if possible, Backtrack otherwise… Depth-First Search 18
19
Algorithm DFS(G) { for each vertex u Є V[G] do { Color[u] := white; Parent[u] := nil; } for each vertex u Є V[G] do if (Color[u] = white) then DFS_Visit(u); } Algorithm DFS_Visit(u) { Color[u] := gray for each vertex v Є Adj[u] do if (Color[v] = white) then { Parent[v] := u; DFS_Visit(v); } Color[u] := black; } white - Unvisited gray - Discovered black - Finished DFS Algorithm 19
20
AC D B E H G F A, B, F, C, G, E Example 20
21
Start from an arbitrary node Visit all the adjacent nodes (distance=1) Visit the nodes adjacent to the visited nodes (distance=2, 3 etc.) Stop when a dead end is met Implemented using a Queue Explore all nodes at distance d… Breadth-First Search (BFS) 21
22
Algorithm BFS(G, s) { for each vertex u Є V[G] – {s} do { Color[u] := white; Distance[u] := ∞; Parent[u] := nil; } Color[s] := gray; Distance[s] := 0; Parent[s] := nil; Q := Ø; Enqueue (Q, s); while (Q ≠ Ø) do { u := Dequeue (Q); for each v Є Adj[u] do if (Color[v] = white) then { Color[v] := gray; Distance[v] := Distance[u] + 1; Parent[v] := u;Enqueue (Q,v); } Color[u] := black; } white - Unvisited gray - Discovered black - Finished s - Source Vertex Q - FIFO Queue BFS Algorithm 22
23
AC D B E H G F A, B, C, E, F, G Example and Analysis 23
24
Depth-first searching 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 A B D E H L M N I O P C F G J K Q N will be found before J 24 LM N OP G Q H J IK FED BC A
25
Breadth-first searching 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 A B C D E F G H I J K L M N O P Q J will be found before N 25 LM N OP G Q H J IK FED BC A
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.