MCS-208 · Unit 8

Graphs

Section 1 of 8

Block 3 · Unit 8Graphs10 min · Section 1 of 8

8.1Graphs: Definitions and Terminology

Covers IGNOU MCS-208 §8.0, 8.2

Trees describe hierarchies, where every item has one parent. Many real relationships are not hierarchical: two cities can be joined by several roads, and you can drive round in a circle. Graphs describe such general relationships.

What a graph is

For example, the graph of Figure 8.1 has V={1,2,3,4,5}V = \{1, 2, 3, 4, 5\} and E={(1,2),(1,5),(1,3),(5,4),(4,3),(2,3)}E = \{(1,2), (1,5), (1,3), (5,4), (4,3), (2,3)\}. We write n=∣V∣n = |V| and e=∣E∣e = |E|.

  • In an undirected graph an edge is an unordered pair: (1,5)(1, 5) and (5,1)(5, 1) are the same edge.
  • In a directed graph (digraph) an edge is an ordered pair (u,v)(u, v), drawn as an arrow from uu to vv; (1,5)(1, 5) and (5,1)(5, 1) are different edges.
  • In a weighted graph each edge has a number (cost, distance, time) called its weight.

Terms

TermMeaning
Adjacentuu and vv are adjacent if there is an edge between them
DegreeNumber of edges at a vertex; in a digraph, in-degree (arrows in) and out-degree (arrows out)
PathA sequence of vertices v0,v1,…,vkv_0, v_1, \dots, v_k in which each consecutive pair is joined by an edge; its length is kk, the number of edges
Simple pathA path in which all vertices are different (except possibly the first and last)
CycleA path of length at least 1 with v0=vkv_0 = v_k (simple, apart from the repeated end)
LoopAn edge (v,v)(v, v) from a vertex to itself
Acyclic graphA graph with no cycles; a directed acyclic graph is called a DAG
Spanning treeA tree containing all nn vertices and n−1n - 1 edges of the graph

In an undirected graph the degrees add up to twice the number of edges, because every edge has two ends: ∑deg⁡(v)=2e\sum \deg(v) = 2e.

Connectivity

  • An undirected graph is connected if there is a path between every pair of vertices.
  • A digraph is strongly connected if for every pair of vertices u,vu, v there is a path from uu to vv and a path from vv to uu.
  • A digraph is weakly connected if it is connected when the directions are ignored, but not strongly connected.
  • A complete graph has an edge between every pair of distinct vertices: an undirected complete graph on nn vertices has n(n−1)2\dfrac{n(n-1)}{2} edges.

Key takeaways

  • G=(V,E)G = (V, E): vertices and edges; undirected (unordered pairs) or directed (ordered pairs); possibly weighted.
  • Path length = number of edges; a cycle starts and ends at the same vertex.
  • In an undirected graph ∑deg⁡(v)=2e\sum \deg(v) = 2e; a complete graph has n(n−1)/2n(n-1)/2 edges.
  • Strongly connected: directed paths in both directions between every pair.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

How many edges does a complete undirected graph on 6 vertices have?

Show answer

Answer: C. 15

n(n−1)/2=6⋅5/2=15n(n - 1)/2 = 6 \cdot 5 / 2 = 15. 30 counts every edge twice (once from each end).

Question 2 · multiple choice

An undirected graph has degrees 4, 3, 3, 2, 2. How many edges does it have?

Show answer

Answer: B. 7

The sum of degrees is 14, which equals 2e2e, so e=7e = 7.

Question 3 · multiple choice

Which statement is correct?

Show answer

Answer: B. Every tree is a graph.

A tree is a connected acyclic graph, a special case of a graph. Graphs in general may contain cycles; trees may not.

Question 4 · multiple choice

In the digraph with edges 1→2, 2→3, 3→1, 3→4, is the graph strongly connected?

Show answer

Answer: D. No, because there is no path from 4 to any other vertex

4 has no outgoing edges, so for example there is no path from 4 to 1. The cycle 1→2→3→1 makes {1, 2, 3} strongly connected, but not the whole graph.

Question 5 · multiple choice

What is the length of the path 1, 3, 4, 5 in an undirected graph?

Show answer

Answer: C. 3

The path uses the edges (1,3), (3,4), (4,5): 3 edges. Weights give the path’s cost, not its length in edges.

Question 6 · short answer

Define a directed graph, an undirected graph, a path, a cycle and a strongly connected graph, with a small example of each.

Show worked answer

A directed graph is G=(V,E)G = (V, E) in which each edge is an ordered pair (u,v)(u, v), drawn as an arrow (e.g. V = {a, b, c}, E = {(a,b), (b,c)}). An undirected graph has unordered edges, so (u,v)=(v,u)(u, v) = (v, u) (e.g. a triangle a–b–c–a). A path is a sequence of vertices in which each consecutive pair is joined by an edge (a, b, c). A cycle is a path of length ≥ 1 whose first and last vertices are equal (a, b, c, a in the triangle). A digraph is strongly connected if every vertex can reach every other vertex by a directed path (e.g. a→b, b→c, c→a).

↑ ↓ move↵ openTab switch type