Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Definitions: Graph, Vertices, Edges

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Question 1

Definitions: Graph, Vertices, Edges

Define a graph G = (V, E) by defining a pair of sets:


1. V = a set of vertices
2. E = a set of edges

Edges:
1. Each edge is defined by a pair of vertices
2. An edge connects the vertices that define it
3. In some cases, the vertices can be the same

o Vertices:
1. Vertices also called nodes
2. Denote vertices with labels

o Representation:
1. Represent vertices with circles, perhaps containing a label
2. Represent edges with lines between circles

o Example:
1. V = {A,B,C,D}
2. E = {(A,B),(A,C),(A,D),(B,D),(C,D)}

Motivation

Many algorithms use a graph representation to represent data or the problem to be


solved

Examples:
o Cities with distances between
o Roads with distances between intersection points
o Course prerequisites
o Network
o Social networks
o Program call graph and variable dependency graph

Graph Classifications

There are seveal common kinds of graphs


o Weighted or unweighted
o Directed or undirected
o Cyclic or acyclic

Choose the kind required for problem and determined by data

We examine each below

Kinds of Graphs: Weighted and Unweighted

Graphs can be classified by whether or not their edges have weights

Weighted graph: edges have a weight


o Weight typically shows cost of traversing

o Example: weights are distances between cities

Unweighted graph: edges have no weight


o Edges simply show connections
o Example: course prereqs

Kinds of Graphs: Directed and Undirected

Graphs can be classified by whether or their edges are have direction


o Undirected Graphs: each edge can be traversed in either direction
o Directed Graphs: each edge can be traversed only in a specified direction

Undirected Graphs

Undirected Graph: no implied direction on edge between nodes


o The example from above is an undirected graph

o
In diagrams, edges have no direction (ie they are not arrows)
o Can traverse edges in either directions

In an undirected graph, an edge is an unordered pair

o Actually, an edge is a set of 2 nodes, but for simplicity we write it with parens

For example, we write (A, B) instead of {A, B}

Thus, (A,B) = (B,A), etc

If (A,B) E then (B,A) E

o Formally: u,v E, (u,v)=(v,u) and u v

A node normally does not have an edge to itself

Directed Graphs

Digraph: A graph whose edges are directed (ie have a direction)


o Edge drawn as arrow
o Edge can only be traversed in direction of arrow
o Example: E = {(A,B), (A,C), (A,D), (B,C), (D,C)}

o Examples: courses and prerequisites, program call graph

In a digraph, an edge is an ordered pair


o Thus: (u,v) and (v,u) are not the same edge
o In the example, (D,C) E, (C,D) E

o What would edge (B,A) look like? Remember (A,B) (B,A)

A node can have an edge to itself (eg (A,A) is valid)

Subgraph

If graph G=(V, E)
o Then Graph G'=(V',E') is a subgraph of G if V' V and E' E

Degree of a Node

The degree of a node is the number of edges the node is used to define

In the example above:


o Degree 2: B and C
o Degree 3: A and D
o A and D have odd degree, and B and C have even degree

Can also define in-degree and out-degree


o In-degree: Number of edges pointing to a node
o Out-degree: Number of edges pointing from a node
o Whare are the in- and out-degree of the example?

Graphs: Terminology Involving Paths

Path: sequence of vertices in which each pair of successive vertices is connected by


an edge

Cycle: a path that starts and ends on the same vertex

Simple path: a path that does not cross itself


o That is, no vertex is repeated (except first and last)

o Simple paths cannot contain cycles

Length of a path: Number of edges in the path


o Sometimes the sum of the weights of the edges

Examples

o A sequence of vertices: (A, B, C, D) [Is this path, simple path, cycle?]


o (A, B, D, A, C) [path, simple path, cycle?]
o (A, B, D, A, C) [path, simple path, cycle?]
o Cycle: ?
o Simple Cycle: ?
o Lengths?
Cyclic and Acyclic Graphs

A Cyclic graph contains cycles


o Example: roads (normally)

An acyclic graph contains no cycles


o Example: Course prereqs!

Examples - Are these cyclic or acyclic?

Connected and Unconnected Graphs and Connected Components

An undirected graph is connected if every pair of vertices has a path between it


o Otherwise it is unconnected
o Give an example of a connected graph

An unconnected graph can be broken in to connected components

A directed graph is strongly connected if every pair of vertices has a path between
them, in both directions

Data Structures for Representing Graphs

Two common data structures for representing graphs:


o Adjacency lists

o Adjacency matrix
Adjacency List Representation

Each node has a list of adjacent nodes

Example (undirected graph):


o A: B, C, D
o B: A, D
o C: A, D
o D: A, B, C

Example (directed graph):


o A: B, C, D
o B: D
o C: Nil
o D: C

Weighted graph can store weights in list

Space: (V + E) (ie |V| + |E|)

Time:
o To visit each node that is adjacent to node u: (degree(u))
o To determine if node u is adjacent to node v: (degree(u))

Adjacency Matrix Representation

Adjacency Matrix: 2D array containing weights on edges


o Row for each vertex
o Column for each vertex
o Entries contain weight of edge from row vertex to column vertex
o Entries contain (ie Integer'last) if no edge from row vertex to column vertex
o Entries contain 0 on diagonal (if self edges not allowed)

Example undirected graph (assume self-edges not allowed):

Example directed graph (assume self-edges allowed):

Can store weights in cells


Space: (V2)

Time:
o To visit each node that is adjacent to node u: (V)
o To determine if node u is adjacent to node v: (1)

Question 2
Application 1
Travelling salesman problem
As a graph problem

Symmetric TSP with four cities


TSP can be modelled as an undirected weighted graph, such that cities are the graph's vertices,
paths are the graph's edges, and a path's distance is the edge's length. It is a minimization
problem starting and finishing at a specified vertex after having visited each
other vertex exactly once. Often, the model is a complete graph (i.e. each pair of vertices is
connected by an edge). If no path exists between two cities, adding an arbitrarily long edge
will complete the graph without affecting the optimal tour.
Asymmetric and symmetric
In the symmetric TSP, the distance between two cities is the same in each opposite direction,
forming an undirected graph. This symmetry halves the number of possible solutions. In
the asymmetric TSP, paths may not exist in both directions or the distances might be different,
forming a directed graph. Traffic collisions, one-way streets, and airfares for cities with
different departure and arrival fees are examples of how this symmetry could break down.
Related problems

An equivalent formulation in terms of graph theory is: Given a complete weighted


graph (where the vertices would represent the cities, the edges would represent the roads,
and the weights would be the cost or distance of that road), find a Hamiltonian cycle with
the least weight.

The requirement of returning to the starting city does not change the computational
complexity of the problem, see Hamiltonian path problem.

Another related problem is the bottleneck traveling salesman problem (bottleneck


TSP): Find a Hamiltonian cycle in a weighted graph with the minimal weight of the
weightiest edge. The problem is of considerable practical importance, apart from evident
transportation and logistics areas. A classic example is in printed circuit manufacturing:
scheduling of a route of the drillmachine to drill holes in a PCB. In robotic machining or
drilling applications, the "cities" are parts to machine or holes (of different sizes) to drill,
and the "cost of travel" includes time for retooling the robot (single machine job
sequencing problem).

The generalized traveling salesman problem deals with "states" that have (one or
more) "cities" and the salesman has to visit exactly one "city" from each "state". Also
known as the "traveling politician problem". One application is encountered in ordering a
solution to the cutting stock problem in order to minimise knife changes. Another is
concerned with drilling in semiconductor manufacturing, see e.g. U.S. Patent 7,054,798.
Surprisingly, Behzad and Modarres demonstrated that the generalised traveling salesman
problem can be transformed into a standard traveling salesman problem with the same
number of cities, but a modified distance matrix.

The sequential ordering problem deals with the problem of visiting a set of cities
where precedence relations between the cities exist.

The traveling purchaser problem deals with a purchaser who is charged with
purchasing a set of products. He can purchase these products in several cities, but at
different prices and not all cities offer the same products. The objective is to find a route
between a subset of the cities, which minimizes total cost (travel cost + purchasing cost)
and which enables the purchase of all required products.

Application 2
Knigsberg Bridge Problem
Knigsberg was a city in Russia situated on the Pregel River, which served as the residence of
the dukes of Prussia in the 16th century. Today, the city is named Kaliningrad, and is a major
industrial and commercial centre of western Russia. The river Pregel flowed through the
town, dividing it into four regions, as in the following picture.

In the eighteenth century, seven bridges connected the four regions. Knigsberg people used
to take long walks through town on Sundays. They wondered whether it was possible to start
at one location in the town, travel across all the bridges without crossing any bridge twice and
return to the starting point. This problem was first solved by the prolific Swiss mathematician
Leonhard Euler, who, as a consequence of his solution invented the branch of mathematics
now known as graph theory. Eulers solution consisted of representing the problem by a
graph with the four regions represented by four vertices and the seven bridges by seven
edges as follows:

Graph Theory is now a major tool in mathematical research, electrical engineering, computer
programming and networking, business administration, sociology, economics, marketing, and
communications; the list can go on and on. In particular, many problems can be modelled
with paths formed by traveling along the edges of a certain graph. For instance, problems of
efficiently planning routes for mail delivery, garbage pickup, snow removal, diagnostics in
computer networks, and others, can be solved using models that involve paths in graphs.
Application 3
Explore diffusion mechanism problem
Graph theory is used in sociology for example to measure actors prestige or to explore
diffusion mechanisms. Graph theory is used in biology and conservation efforts where a
vertex represents regions where certain species exist and the edges represent migration path or
movement between the regions. This information is important when looking at breeding
patterns or tracking the spread of disease, parasites and to study the impact of migration that
affect other species. Graph theoretical concepts are widely used in Operations Research. For
example, the traveling salesman problem, the shortest spanning tree in a weighted graph,
obtaining an optimal match of jobs and men and locating the shortest path between two
vertices in a graph.

You might also like