This document defines key graph concepts like paths, cycles, degrees of vertices, and different types of graphs like trees, forests, and directed acyclic graphs. It also describes common graph representations like adjacency matrices and lists. Finally, it covers graph traversal algorithms like breadth-first search and depth-first search, outlining their time complexities and providing examples of their process.
2. Paths and cycles
â A path is a sequence of nodes
v1, v2, âŠ, vN such that (vi,vi+1)ïE for 0<i<N
â The length of the path is N-1.
â Simple path: all vi are distinct, 0<i<N
â A cycle is a path such that v1=vN
â An acyclic graph has no cycles
4. More useful definitions
â The degree of a vertex is the number of
edges incident to that vertex
â In a directed graph:
â The indegree of a node v is the number of
distinct edges (w,v)ïE.
â The outdegree of a node v is the number of
distinct edges (v,w)ïE.
â A node with indegree 0 is a root.
5. Trees are graphs
â A dag is a directed acyclic graph.
â A tree is a connected acyclic undirected graph.
â A forest is an acyclic undirected graph (not
necessarily connected), i.e., each connected
component is a tree.
6. Even More Terminology
â subgraph: subset of vertices and edges forming a graph
â connected component: maximal connected subgraph. E.g., the graph below has
3 connected components.
connected not connected
âąconnected graph: any two vertices are connected by some path
8. More Connectivity
n = #vertices
m = #edges
â For a tree m = n - 1
n ïœ 5
m ïœ 4
n ïœ 5
m ïœ 3
If m < n - 1, G is not
connected
9. G = (V, E)
a vertex may have:
0 or more predecessors
0 or more successors
10. some problems that can be
represented by a graph
â computer networks
â airline flights
â road map
â course prerequisite structure
â tasks for completing a job
â flow of control through a program
â many more
11. Graphs in Computer Science: How
do they help?
â What do they model?
â An abstraction in Core CS.
â Examples: VLSI Circuits, Communication Networks, Logical
flow of a computer program, Data structures.
â An abstraction for data and relationships.
â Examples: The Web, Social Networks, Flows and Flow
Networks, Biological Data, Taxonomies, Citations, Explicit
relations within a DB system.
â What aspects are studied?
â Algorithms, Data Structures and Complexity Theory.
â Characterization and Modeling of Graphs.
â Implementations of Graph Algorithms in Specific
contexts.
12. What is a graph?
â A set of vertices and edges
â Directed/Undirected
â Weighted/Unweighted
â Cyclic/Acyclic
vertex
edge
13. Representation of Graphs
â Adjacency Matrix
â A V x V array, with matrix[i][j] storing whether
there is an edge between the ith vertex and the jth
vertex
â Adjacency Linked List
â One linked list per vertex, each storing directly
reachable vertices
15. graph variations
â undirected graph (graph)
â edges do not have a direction
â (V1, V2) and (V2, V1) are the same edge
â directed graph (digraph)
â edges have a direction
â <V1, V2> and <V2, V1> are different edges
â for either type, edges may be weighted or
unweighted
16. a digraph
A
B
C D
E
V = [A, B, C, D, E]
E = [<A,B>, <B,C>, <C,B>, <A,C>, <A,E>, <C,D>]
17. graph data structures
â storing the vertices
â each vertex has a unique identifier and, maybe,
other information
â for efficiency, associate each vertex with a number
that can be used as an index
â storing the edges
â adjacency matrix â represent all possible edges
â adjacency lists â represent only the existing edges
18. storing the vertices
â when a vertex is added to the graph, assign it
a number
â vertices are numbered between 0 and n-1
â graph operations start by looking up the
number associated with a vertex
â many data structures to use
â any of the associative data structures
â for small graphs a vector can be used
â search will be O(n)
23. many graphs are âsparseâ
â degree of âsparsenessâ key factor in
choosing a data structure for edges
â adjacency matrix requires space for all
possible edges
â adjacency list requires space for existing
edges only
â affects amount of memory space needed
â affects efficiency of graph operations
25. Adjacency Lists
A representation of the graph consisting of a list of nodes, with
each node containing a list of its neighboring nodes.
This representation takes O(|V | + |E|) space.
27. Introduction
â A free tree is a connected undirected graph without a cycle.
â Note: This definition of tree is different from the one of a rooted tree
â In a free tree |E| = |V| - 1
â Example of a free tree:
â A forest is an acyclic directed or undirected graph consisting of
two or more trees
â The trees in a directed forest are rooted trees
â The trees in an undirected forest are free trees
29. Use of a stack
â It is very common to use a stack to keep track
of:
â nodes to be visited next, or
â nodes that we have already visited.
â Typically, use of a stack leads to a depth-first
visit order.
â Depth-first visit order is âaggressiveâ in the
sense that it examines complete paths.
30. Use of a queue
â It is very common to use a queue to keep track
of:
â nodes to be visited next, or
â nodes that we have already visited.
â Typically, use of a queue leads to a breadth-
first visit order.
â Breadth-first visit order is âcautiousâ in the
sense that it examines every path of length i
before going on to paths of length i+1.
31. Breadth-First Search (BFS)
â BFS tries to search all paths.
â BFS makes use of a queue to store visited (but
not dead) vertices, expanding the path from the
earliest visited vertices.
49. Example
â Breadth-first traversal using a queue.
Order of
Traversal Queue rearA B D E C G F H I
Queue front
A
B D E
C G
F H
I
BFS-tree:
Note: The BFS-tree for undirected graph is a free tree
50. Analysis of BFS
For a Graph G=(V, E) and n = |V| and m=|E|
âą When Adjacency List is used
ïComplexity is O(m + n)
âą When Adjacency Matrix is used
ï Scanning each row for checking the connectivity of a Vertex
is in order O(n).
ïSo, Complexity is O(n2)
51. Depth-First Search (DFS)
â Strategy: Go as far as you can (if you have not
visit there), otherwise, go back and try another
way
72. Example
â Depth-first traversal using an explicit stack.
Order of
Traversal StackA B C F E G D H I
The Preorder Depth First Tree:
Note: The DFS-tree for undirected graph is a free tree
73. Recursive preorder Depth-First Traversal Tracing
At each stage, a set of unvisited adjacent
vertices of the current vertex is generated.
The Preorder Depth First Tree:
74.
75.
76.
77.
78. Analysis of DFS
For a Graph G=(V, E) and n = |V| and m=|E|
âą When Adjacency List is used
ïComplexity is O(m + n)
âą When Adjacency Matrix is used
ï Scanning each row for checking the connectivity of a Vertex
is in order O(n).
ïSo, Complexity is O(n2)
DFS uses space O(|V|) in the worst case to store the stack
of vertices on the current search path as well as the set of
already-visited vertices.