


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



















Department of Computer ScienceUniversity of Maryland, College Park
Graph Data Structures Graph Definitions Graph Operations Breadth-first Search (BFS) Traversals Orders Depth-first Search (DFS) Traversal Algorithms BFS vs. DFS Traversal
Node
Element of graph State
List of adjacent nodes Edge
Connection between two nodes State
Endpoints of edge
Directed graph
Directed edges Undirected graph
Undirected edges
Path
Sequence of nodes n
, n 1
, … n 2
k
Edge exists between each pair of nodes n
, ni i+
Example
A, B, C
is a path
Path
Sequence of nodes n
, n 1
, … n 2
k
Edge exists between each pair of nodes n
, ni i+
Example
A, B, C
is a path A, E, D
is not a path
Cycle
Path that ends back at starting node Example
A, E, A A, B, C, D, E, A Simple path
No cycles in path Acyclic graph
No cycles in graph
Reachable
Path exists between nodes Connected graph
Every node is reachable from some node in graph
Unconnected graphs
Approach
Visit all neighbors of nodefirst View as series ofexpanding circles Keep list of nodes to visitin queue Example traversal
n a, c, b e, g, h, i, j d, f
Example traversals starting from 1
Left to right
Right to left
Random
Approach
Visit all nodes on path first Backtrack when path ends Keep list of nodes to visit ina stack Example traversal
n, a, b, c, d, … f …
Example traversals from 1
Left to right
Right to left
Random
Initialize { Visited } to empty set Add to { Visited } as nodes is visited Skip nodes already in { Visited }
Initialize tag on all nodes (to False) Set tag (to True) as node is visited Skip nodes with tag = True