0% found this document useful (0 votes)
80 views100 pages

Dagajal 150302145746 Conversion Gate02

Paul Dirac said that in poetry, the goal is to convey something novel to everyone in a way that can be universally understood, which is the opposite of the goal in science, which is to convey novel information to people.

Uploaded by

Neha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views100 pages

Dagajal 150302145746 Conversion Gate02

Paul Dirac said that in poetry, the goal is to convey something novel to everyone in a way that can be universally understood, which is the opposite of the goal in science, which is to convey novel information to people.

Uploaded by

Neha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 100

In science one tries to tell people, in such a way

as to be understood by everyone, something


that no one ever knew before.
But in poetry, it's the exact opposite.
Paul Dirac
Graph
edge
vertex
Weighted Graph
1 3

5 0

-2

5
Undirected Graph
Complete Graph (Clique)
Path
Length = 4 a

d e
Cycle
a

b g

c f

d e
Directed Acyclic Graph (DAG)
Degree
In-Degree Out-Degree
Disconnected Graph
Connected Components
Formally
A weighted graph G = (V, E, w), where
 V is the set of vertices
 E is the set of edges
 w is the weight function
Variations of (Simple) Graph
Multigraph
More than one edge between two
vertices
E is a bag, not a set
Hypergraph
Edges consists of more than two vertex
Example
V = { a, b, c }
E = { (a,b), (c,b), (a,c) }
w = { ((a,b), 4), ((c, b), 1), ((a,c),-3) }

a
-3 4

c b
1
Adjacent Vertices
adj(v) = set of vertices adjacent to v
adj(a) = {b, c}
adj(b) = {}
a
adj(c) = {b}
-3 4

∑ |adj(v)| = |E|
v
c
1
b

adj(v): Neighbours of v
direct
city flight

cost
Question
What is the shortest way to travel between A and
B?
“SHORTEST PATH PROBLEM”

How to mimimize the cost of visiting n cities such


that we visit each city exactly once, and finishing
at the city where we start from?
“TRAVELING SALESMAN PROBLEM”
network
computer link
Question
What is the shortest route to send a packet from A to
B?

“SHORTEST PATH PROBLEM”


web link
web page
prerequisite
module
Question
Find a sequence of modules to take that satisfy the
prerequisite requirements.

“TOPOLOGICAL SORT”
Other Applications
Biology
VLSI Layout
Vehicle Routing
Job Scheduling
Facility Location
:
:
Adjacency Matrix
double vertex[][];
1
1 2 3 -3 4

1 ∞ 4 -3 3 2
1
2 ∞ ∞ ∞

3 ∞ 1 ∞
Adjacency List
EdgeList vertex[];

1 3 -3 2 4
1
2 -3 4

3 2
3 2 1 1

neighbour cost
“Avoid Pointers in Competition..”

1 2 3
2 1
3 2 -3 4

3 2
1 4 -3 1
2
3 1
A

B C

F E D
A

B C

F E D
A

B C

F E D
A

B C

F E D
A

B C

F E D
A

B C

F E D
A

B C

F E D
A

B C

F E D
0

2 1

3 2 2
Level-Order on Tree
if T is empty return 1
Q = new Queue
2 3
Q.enq(T)
while Q is not empty 4 5 6 7
curr = Q.deq()
print curr.element 8 9 0

if T.left is not empty


Q.enq(curr.left)
if curr.right is not empty
Q.enq(curr.right)
Calculating
Q = new Queue
Level
Q.enq (v) A
v.level = 0
while Q is not empty B C
curr = Q.deq()
if curr is not visited F E D
mark curr as visited
foreach w in adj(curr)
if w is not visited
w.level = curr.level + 1
Q.enq(w)
Search All Vertices
Search(G)
foreach vertex v
mark v as unvisited
foreach vertex v
if v is not visited
BFS(v)
A

B C

F E D
A

B C

F E D
A

B C

F E D
Applications
BFS
shortest path

DFS
longest path in DAG
finding connected component
detecting cycles
topological sort
Definition
A path on a graph G is a sequence of vertices v0, v1,
v2, .. vn where (vi,vi+1)∈E

The cost of a path is the sum of the cost of all


edges in the path. A

B C

F E D
A

B C

F E D
ShortestPath(s)
Run BFS(s)
w.level: shortest distance from s
w.parent: shortest path from s
A
1 5

3
B C
3 1 5
1
F E D
4 2
BFS(s) does not work
Must keep track of smallest distance so far.

If we found a new, shorter path, update the distance.


s w
10
8

2
6

v
Definition
distance(v) : shortest distance so far from s to v

parent(v) : previous node on the shortest path so far


from s to v

cost(u, v) : the cost of edge from u to v


s w
10
8

2
6
distance(w) = 8
v
cost(v,w) = 2

parent(w) = v
A
1 5

3
B C
3 1 5
1
F E D
4 2
0
1 5

8 3

8
3 1 5
1
8

8
4 2
0
1 5

3
5
8

3 1 5
1
8

8
4 2
0
1 5

3
5
8

3 1 5
1
8

8
4 2
0
1 5

3
8 5
3 1 5
1
6 10
8

4 2
0
1 5

3
8 5
3 1 5
1
6 10
8

4 2
0
1 5

3
8 5
3 1 5
1
10 6 8
4 2
0
1 5

3
8 5
3 1 5
1
10 6 8
4 2
0
1 5

3
8 5
3 1 5
1
10 6 8
4 2
0
1 5

3
8 5
3 1 5
1
10 6 8
4 2
0
5

3
8 5
1

10 6 8
4 2
Dijkstra’s Algorithm
color all vertices yellow
foreach vertex w
distance(w) = INFINITY
distance(s) = 0
Dijkstra’s Algorithm
while there are yellow vertices
v = yellow vertex with min distance(v)
color v red
foreach neighbour w of v
relax(v,w)
Using Priority Queue
foreach vertex w
distance(w) = INFINITY
distance(s) = 0
pq = new PriorityQueue(V)

while pq is not empty


v = pq.deleteMin()
foreach neighbour w of v
relax(v,w)
Initialization O(V)
foreach vertex w
distance(w) = INFINITY
distance(s) = 0
pq = new PriorityQueue(V)
Main Loop
while pq is not empty
v = pq.deleteMin()
foreach neighbour w of v
relax(v,w)
0
1 5

8 -3

8
3 1 3
1
8

8
-4 -2
0
1 5

-3
5
8

3 1 3
1
8

8
-4 -2
0
1 5

-3
2 5
3 1 3
1
6 8
8

-4 -2
0
1 5

-3
2 5
3 1 3
1
2 3 4
-4 -2
0
1 5

-3
2 5
3 1 3
1
-1 3 1
-4 -2
Bellman-Ford Algorithm
do |V|-1 times
foreach edge (u,v)
relax(u,v)

// check for negative weight cycle


foreach edge (u,v)
if distance(v) > distance(u) + cost(u,v)
ERROR: has negative cycle
Idea: Use DFS
Denote vertex as “visited”, “unvisited” and “visiting”

Mark a vertex as “visited” = vertex is finished


unvisited

visiting A
visited

B C

F E D
unvisited

visiting A
visited

B C

F E D
unvisited

visiting A
visited

B C

F E D
unvisited

visiting A
visited

B C

F E D
unvisited

visiting A
visited

B C

F E D
unvisited

visiting A
visited

B C

F E D
unvisited

visiting A
visited

B C

F E D
unvisited

visiting A
visited

B C

F E D
unvisited

visiting A
visited

B C

F E D
unvisited
Tree Edge
visiting

visited

Backward Edge

Forward Edge

Cross Edge
(in a DAG)
A

B C

F E D

1 0
unvisited

visiting A
visited

B C

F E D
2 1 0
unvisited

visiting A
visited
3

B C

F E D
2 1 0
unvisited

visiting 5
A
visited
3 4

B C

F E D
2 1 0
Longest Path
Run DFS as usual
When a vertex v is finished, set
L(v) = max {L(u) + 1} for all edge (v,u)
Topological Sort
Goal: Order the vertices, such that if there is a path
from u to v, u appears before v in the output.
Topological Sort
 ACBEFD A

 ACBEDF B C
 ACDBEF
F E D
Topological Sort
Run DFS as usual
When a vertex is finish, push it onto a stack

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy