Document 403240

CSE 246
Data Structures and Algorithms
Graphs
Graph data structure
ο‚— Graph is an ADT.
ο‚— A graph is a set of vertices and edges
𝐺 = {𝑉, 𝐸}
𝑉={v1,v2,v3,…,vn},
𝐸={e1,e2,e3,…,en}
ο‚— An Edge connects exactly two vertices.
ο‚— A vertex can have any number of edges connected to it.
2
Quratulain
Graphs
ο‚— Edge might be directed or undirected.
ο‚— Weighted graphs. Additional information on edges.
3
Quratulain
Terms to remember
ο‚— graph
ο‚— path
ο‚— vertex (pl vertices)‫‏‬
ο‚— cycle
ο‚— edge
ο‚— Hamiltonian
ο‚— edge cost
ο‚— Eulerian
ο‚— undirected graph
ο‚— planar graph
ο‚— directed graph
ο‚— acyclic directed graph
ο‚— simple graph
ο‚— forest
ο‚— loop
ο‚— tree
Graph Applications
ο‚— Modelling a road network with vertexes as towns and edge costs as
ο‚—
ο‚—
ο‚—
ο‚—
distances.
Modelling a water supply network. A cost might relate to current
or a function of capacity and length. As water flows in only 1 direction,
from higher to lower pressure connections or downhill, such a network
is inherently an acyclic directed graph.
Internet traffic modelling. Dynamically modelling the status of a
set of routes by which traffic might be directed over the Internet.
Flight scheduling. Minimising the cost and time taken for air travel
when direct flights don't exist between starting and ending airports.
Using a directed graph to map the links between pages within a
website and to analyse ease of navigation between different parts of the
site.
Graph Applications
ο‚— The Seven Bridges of Königsberg is a problem inspired by an
actual place and situation.
ο‚— The question is whether it is possible to walk with a route
that crosses each bridge exactly once, and return to the
starting point. In 1736, Leonhard Euler proved that it was
not possible."
Source: http://en.wikipedia.org/wiki/Seven_Bridges_of_Koenigsberg
6
Quratulain
Three cottage problem
Source: http://en.wikipedia.org/wiki/Three_cottage_problem
"The three cottage problem is a well-known mathematical
puzzle. It can be stated like this:
Suppose there are three cottages on a plane (or sphere) and
each needs to be connected to the gas, water, and electric
companies. Is there a way to do so without any of the lines
crossing each other?"
Three cottage problem
"The three cottage problem is a well-known mathematical
puzzle. It can be stated like this:
Suppose there are three cottages on a plane (or sphere) and
each needs to be connected to the gas, water, and electric
companies. Is there a way to do so without any of the lines
crossing each other?"
ο‚— Source: http://en.wikipedia.org/wiki/Three_cottage_problem
8
Quratulain
Data structure to store Graphs
ο‚— To select data structure for graph:
ο‚— First programmer need to understand the application data.
ο‚— Then construct a graph.
ο‚— The choice and design of data structure(s) will influence and be
influenced by program performance and memory usage
requirements.
9
Quratulain
Data structures to store graph
ο‚— Adjacency list
ο‚— Adjacency matrix.
ο‚— Incident list
Source:
http://interactivepython.org/courselib/static/pythonds/Graphs/graphintro.html
10
Quratulain
Adjacency matrix
ο‚— implement a graph is to use a two-dimensional matrix.
11
Quratulain
Adjacency list
ο‚— A more space-efficient way to implement a sparsely
connected graph is to use an adjacency list
12
Quratulain
Graph search algorithms
ο‚— Breadth first search
ο‚— Depth first search
13
Quratulain
Depth first search
ο‚— The depth-first search uses a stack to remember where it
should go when it reaches a dead end.
14
Quratulain
Depth first search Example
ο‚— Knight’s tour graph
15
Quratulain
Breadth first search
ο‚— the algorithm likes to stay as close as possible to the starting
point. It visits all the vertices adjacent to the starting vertex,
and only then goes further. This kind of search is
implemented using a queue
16
Quratulain
Breadth first search Example
ο‚— Word ladder graph
17
Quratulain
Graph Analysis
18
Quratulain