CmSc 250 Intro to Algorithms
Graph Algorithms - Basic Concepts
- Basic graph concepts
A graph is a mathematical object that is used to model different situations – objects and processes:
Linked list
Tree (special type of graph
Flowchart of a program
Structure chart of a program
Finite state automata
City map
Electric circuits
Course curriculum
Vertices: can have names and properties
Edges: connect two vertices, can be labeled,
can be directed
Adjacent vertices: if there is an edge between them.
Example:
Vertices: A,B,C,D
Edges:AB, AC, BC, CD
Graph1:
Same graph given in another way:
There are two basic types of graphs - directed and undirected.
In undirected graphs the edges are symmetrical, e.g.
if A and B are vertices,
A B and B A are one and the same edge.
Graph1 above is undirected.
Examples of directed graphs
|
Graph2: |
Graph3: |
Graph2 and Graph3 are different graphs
Some definitions of basic graph concepts differ slightly depending on whether we talk about directed or undirected graphs.
A path is a list of vertices in which successive vertices are connected by edges
Examples
Some paths in Graph1 :
A B C D
A C B A C D
A B
D C B
C B A
Some paths in Graph2:
Note: In different textbooks you may find different definitions for a path in a graph. Apart from the usual confusion, there is nothing wrong in this as long as the definitions are followed throughout the presented theory.
Examples:
In Graph1, D C B A is a simple path, while D C B A C is not a simple path
In Graph2, D A B is a simple path, while D A D B is not a simple path
Examples:
Cycles in Graph1: C A B C, C B A C, A B C A, A C B A,
B A C B, B C A B
A B A is not a cycle, because the edge A B is the same as B A
Cycles in Graph3: A D A, D A B D
A graph without cycles is called acyclic graph
Connected graph: There is a path between each two vertices
Graph1, Graph2 and Graph3 are connected graphs.
Disconnected graph: There are at least two vertices not connected by a path.
Examples of disconnected graphs:
|
Graph4 |
|
Graph5 |
|
|
Vertices: |
A,B,C,D |
Vertices: |
A,B,C,D |
|
Edges: |
AB, CD |
Edges: |
AB, AC |
Note: in a tree, when we choose a root we impose an orientation.
Given an acyclic graph, we may choose any node to be the root of a tree.
Example
Acyclic graph:
If we choose vertex a to be the root, then we get the following tree:
If we choose c to be the root, then the tree will be:
Note also, that the graph does not specify the order of the children of a given node.
A spanning tree of an undirected graph is a subgraph
that
contains all the vertices, and no cycles.
If we add any edge to the spanning tree, it forms a cycle,
and the tree becomes a graph.
It is possible to define a spanning tree for directed graphs,
however the definition is rather complicated and will
not be discussed here.
Example:
Spanning trees for Graph1:
A tree with N vertices has N-1 edges.
A graph with less than N-1 edges is not connected.
Example:
Dense graphs: relatively few of the possible edges are
missing
Sparse graphs: relatively few of the possible
edges are present
Note: Some textbooks define networks to be undirected weighted graphs as well.
Examples:
Vertices: A,B,C,D
Edges: AB, AC, BD, CD
A B C D A 0 1 1 0 B 1 0 0 1 C 1 0 0 1 D 0 1 1 0Note:
Each vertex is associated with a list, that holds all adjacent vertices.
A: B, C
B: A, D
C: A, D
D: B, C
Other information, as weights, labels, names, can be represented using auxiliary arrays.
Learning Goals
Exam-like questions