Undirected Graphs
Undirected Graphs
•Vertices: websites
•Edges: hyperlinks
10 million Facebook friends
Vertices: People,
Edges: Relationships
Weight: Level of interaction among people
The Internet as mapped by the
Opte Project
Nodes: Active
Devices
Computers
Switches
Routers
Edges: (Passive
components)
Connections
Weight:
bandwidth,
latency …
Graph Applications
Graph Terminology
Path. Sequence of vertices
connected by edges.
Cycle. Path whose first and
last vertices are the same.
A tree is a graph without a
cycle
Two vertices are connected if
there is a path between them.
Degree of a vertex: number of
connections to/from a vertex
Some Graph-processing Problems
Types of Graphs
Lecture Outline
introduction
graph API and Representation
depth-first search
breadth-first search
Graph Representation
Graph drawing. Provides intuition about the
structure of the graph.
Vertex Representation
Use integers between 0 and V – 1.
Applications: convert between names and
integers with symbol table.
Graph API
Graph representation: set of edges
Adjacency-list
Takes O(V+E)
Good choice for many
applications
Most real world matrices are
sparse
Adjacency-list graph representation:
Java implementation
Lecture Outline
introduction
graph API and Representation
depth-first search
breadth-first search
Graph Search
Goal: Given a starting vertex, s, find all
vertices connected to s
Two Commonly used Search Algorithms:
Depth First Search(DFS)
Breadth First Search(BFS)
Wide range of graph problems can be solved
using graph search
Finding a path (checking connectivity)
Cycle detection
Finding Minimum Spanning Trees
Depth First Search
Analogy:Preparing for a date
Depth First Search –the idea
The algorithm aggressively moves away from
the starting node.
We only go back when we there is a dead end
Depth First Search –
Implementation Algorithm
DFS may use a stack to remember where it
should go when it reaches a dead end.
Steps in DFS:
Step 1: Pick a starting node
Step 2: If possible, visit an adjacent unvisited
vertex, mark it, and push it on the stack.
Step 3: If there are no unvisited nodes, then pop a
vertex off the stack, if it is not empty, and repeat
step 2
Step 4: If stack is empty, you are done
It’s possible to have a recursive
implementation instead of using a stack
DFS Application: Finding a Path
Property of DFS: During DFS, the stack
contains the path taken to reach at the
current node
This can be used to quickly determine a path
from one vertex to another.
Example use cases:
finding driving directions.
checking connectivity in a network
Lecture Outline
introduction
graph API and Representation
depth-first search
breadth-first search
Breadth First Search –the idea
Unlike DFS, BFS tries to stay as close to the
starting position as possible
It only then goes further when all the vertices
adjacent to the starting vertex are visited
Breadth First Search -
Implementation
Uses a queue instead of a stack
Steps in BFS:
Step 1: Pick a starting node
Step 2: If possible, visit an adjacent unvisited
vertex, mark it, and insert it into the queue
Step 3: If there are no unvisited adjacent nodes,
then remove a vertex from the queue.
Step 4a: if queue is not empty go to step 2.
Step 4b: If the queue is empty, you are done
BFS Application: Shortest Path
Property of BFS: It first finds all the vertices
that are one edge away from the starting
point, then all the vertices that are two edges
away, and soon.
When you find the specified vertex using BFS,
you know the path you’ve traced so far is the
shortest path to the node.
This is only true for unweighted graphs (we will
discuss the case for weighted graphs later)
Incommunicationnetworks,itcanbeusedtofind
theroutewithminimumhops
FURTHER APPLICATIONS
OF SEARCH
Determine a Minimum Spanning Tree
Check the existence of cycles in a graph