0% found this document useful (0 votes)
41 views9 pages

Graphes

The document contains definitions and procedures for representing and working with graphs in Ada. It defines a graph data structure and procedures for inserting nodes and arcs, finding the shortest path between two nodes using Dijkstra's algorithm, and finding the maximum flow in a graph. It includes definitions for node and arc data types, tables and lists for storing graph data, and initialization procedures for setting up the data structures for graph algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views9 pages

Graphes

The document contains definitions and procedures for representing and working with graphs in Ada. It defines a graph data structure and procedures for inserting nodes and arcs, finding the shortest path between two nodes using Dijkstra's algorithm, and finding the maximum flow in a graph. It includes definitions for node and arc data types, tables and lists for storing graph data, and initialization procedures for setting up the data structures for graph algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

with text_io ; --use text_io ;

with Ada.Streams.Stream_IO; use Ada.Streams;


with temp_avls ;
with temp_tables ;
with temp_liste_triees ;
with entiers ; use entiers ;
with temp_tab_tries ;
with temp_listes ;
package body graphes is
-----------------------------------------------------------------------------
procedure ins_arc ( g : in out graphe ; n1, n2 : item_id ; d : item_arc ) is
an1, an2 : graphe ;
e : boolean ;

begin

ins ( n1, g, an1, e ) ;


if not ( e ) then
init ( an1.attr ) ;
end if ;
ins ( n2, g, an2, e ) ;
ins ( n2, d, an1.attr ) ;
end ins_arc ;
-----------------------------------------------------------------------------
procedure ins_arc2 ( g : in out graphe ; n1, n2 : item_id ; d : item_arc ) is
begin
ins_arc ( g, n1, n2, d ) ;
ins_arc ( g, n2, n1, d ) ;
end ins_arc2 ;
---------------------------------------------------------
---------------------------------------------------------
--procedure put ( x : arc_cell ) is
--begin
-- text_io.put ( " " ) ;
-- put ( x.etiq ) ;
-- text_io.put ( ", " ) ;
-- put ( x.nsuiv ) ;
--end put ;
--------------------------------------------------
--procedure write ( fs : access Ada.Streams.Root_Stream_Type'Class ; x :
arc_cell ) is
--begin
-- write ( fs, x.etiq ) ;
-- write ( fs, x.nsuiv ) ;
--end write ;
--------------------------------------------------
--procedure read ( fs : access Ada.Streams.Root_Stream_Type'Class ; x : out
arc_cell ) is
--begin
-- read ( fs, x.etiq ) ;
-- read ( fs, x.nsuiv ) ;
--end read ;
--------------------------------------------------
--procedure put ( x : arcs.liste ) is
--begin
-- arcs.put ( x ) ;
--end put ;
-----------------------------------------------------
--procedure write ( fs : access Ada.Streams.Root_Stream_Type'Class ; x :
arcs.liste ) is
--begin
-- arcs.write ( fs, x ) ;
--end write ;
--------------------------------------------------
--procedure read ( fs : access Ada.Streams.Root_Stream_Type'Class ; x : out
arcs.liste ) is
--begin
-- arcs.read ( fs, x ) ;
--end read ;
------------------------------------------------------------------
function pcm ( g : graphe ; a, z : item_id ) return ids_liste is
y : ids_liste ;
-- on aurait du creer un nouveau type :
type dist_prec is record
dist : item_arc ;
prec : item_id ; end record ;

package da_pack is new temp_tables ( item_id, dist_prec ) ; use da_pack ;

da, da_ns, da_x : da_pack.table ;


g_x : graphe ;
x, ns : item_id ;
s : arcs.table ;
ax, az, as : item_arc ;
e : boolean ;
package f_pack is new temp_tables ( item_arc, item_id ) ; use f_pack ;
f : f_pack.table ;
begin
init ( y ) ;
init ( da ) ;
init ( f ) ;
if g /= null and a /= z and adr ( a, g ) /= null and adr ( z, g ) /= null
then
x := a ; ax := zero ; az := infini ;
loop
g_x := adr ( x, g ) ;
s := g_x.attr ;
loop
exit when estvide ( s ) ;
ns := s.cle ;
as := ax + s.attr ;
if as < az then
ins ( ns, da, da_ns, e ) ;
if ( not e ) or else da_ns.attr.dist > as then
da_ns.attr.dist := as ; da_ns.attr.prec := x ;
if ns = z then
az := as ;
else
ins ( as, ns, f ) ;
end if ;
end if ;
end if ;

s := s.alt ;
end loop ;
exit when estvide ( f ) ;
del_min ( ax, x, f ) ;
end loop ;
x := z ;
loop
exit when x = a ;
da_x := adr ( x, da ) ;
--ins1 ( ( da_x.attr.dist, da_x.attr.prec ), y ) ;
ins1 ( da_x.attr.prec, y ) ;
x := da_x.attr.prec ;
end loop ;
end if ;

return y ;
end pcm ;
--------------------------------------------------------------
--------------------------------------------------------------
--type id_id is record
-- id1, id2 : item_id ; end record ;
procedure put ( x : id_id ) is begin
put ( x.id1 ) ; text_io.put ( "--" ) ;
put ( x.id2 ) ; end put ;
-----------------------------------------------------------------------------
procedure read ( fs : access Ada.Streams.Root_Stream_Type'Class ; x : out
id_id ) is begin
id_id'read ( fs, x ) ; end read ;
-----------------------------------------------------------------------------
procedure write ( fs : access Ada.Streams.Root_Stream_Type'Class ; x : id_id )
is begin
id_id'write ( fs, x ) ; end write ;
-----------------------------------------------------------------------------
function "<" ( x1 : id_id ; x2 : id_id ) return boolean is begin
if x1.id1 < x2.id1 then
return true ;
elsif x1.id1 = x2.id1 then
if x1.id2 < x2.id2 then
return true ;
elsif x1.id2 = x2.id2 then
return false ;
else
return false ;
end if ;
else
return false ;
end if ;
end "<" ;
-----------------------------------------------------------------------------
function "=" ( x1 : id_id ; x2 : id_id ) return boolean is begin
return x1.id1 = x2.id1 and x1.id2 = x2.id2 ; end "=" ;
-----------------------------------------------------------------------------
function ">=" ( x1 : id_id ; x2 : id_id ) return boolean is begin
return not ( x1 < x2 ) ; end ">=" ;
-----------------------------------------------------------------------------
function ">" ( x1 : id_id ; x2 : id_id ) return boolean is begin
if x1.id1 < x2.id1 then
return false ;
elsif x1.id1 = x2.id1 then
if x1.id2 < x2.id2 then
return false ;
elsif x1.id2 = x2.id2 then
return false ;
else
return true ;
end if ;
else
return true ;
end if ; end ">" ;
-----------------------------------------------------------------------------
function "<=" ( x1 : id_id ; x2 : id_id ) return boolean is begin
return not ( x1 > x2 ) ; end "<=" ;
--package id_id_caps is new tables ( id_id, item_arc ) ;
use id_id_caps ;
----------------------------------------------------------------
package arc2s is new
temp_liste_triees ( item_arc, entier ) ; use arc2s ;
type noeud_cell2 is record
cap_noeud : item_arc ;
recu : item_arc ;
flux_noeud : item_arc ;
suiv_trie : arc2s.table ;
prec_trie : arc2s.table ; end record ;
--procedure put ( x : noeud_cell2 ) is
--begin
-- text_io.put ( "cap : " ) ; put ( x.cap_noeud ) ; text_io.put ( ", " ) ;
-- text_io.put ( "re�u : " ) ; put ( x.recu ) ; text_io.put ( ", " ) ;
-- text_io.put ( "flux : " ) ; put ( x.flux_noeud ) ; text_io.put ( ", " ) ;
--text_io.new_line ;
--text_io.put ( "suiv : " ) ; put ( x.suiv_trie ) ; text_io.new_line ;
--text_io.put ( "prec : " ) ; put ( x.prec_trie ) ;
--end put ;

--procedure read ( fs : access Ada.Streams.Root_Stream_Type'Class ; x : out


noeud_cell2 ) is begin
-- noeud_cell2'read ( fs, x ) ; end read ;
--procedure write ( fs : access Ada.Streams.Root_Stream_Type'Class ; x :
noeud_cell2 ) is begin
-- noeud_cell2'write ( fs, x ) ; end write ;

-------------------------------------------------------------------
-------------------------------------------------------------------
function flot_max ( g : graphe ) return id_id_liste is
flux : id_id_liste ;
n : entier := card ( g ) ;
package noeud_cell2s is new
temp_tab_tries ( n, item_id, noeud_cell2 ) ;
use noeud_cell2s ;
h : noeud_cell2s.table ;
package cap_ids is new temp_liste_triees ( item_arc, entier );
use cap_ids ;
f : cap_ids.table ;
--------------------------------------------------------------
procedure init
( g : graphe ; h : in out noeud_cell2s.table ;
f : in out cap_ids.table ; flux : in out id_id_liste ) is
i : entier ;
procedure init1( g:graphe ; h:in out noeud_cell2s.table ) is
begin
if not estvide(g) then
init1 ( s1(g), h ) ;
i := i + 1 ;
h(i).cle := g.cle ;
h(i).attr.cap_noeud := zero ;
h(i).attr.recu := zero ;
h(i).attr.flux_noeud := zero ;
init ( h(i).attr.suiv_trie ) ;
init ( h(i).attr.prec_trie ) ;
init1 ( s2(g), h ) ;
end if ;
end init1 ;
--------------------------------------------------------------
procedure init2
( g : graphe ; h : in out noeud_cell2s.table ;
flux : in out id_id_liste) is
s : arcs.table ;
x, ns : entier ;
i_i : id_id ;
begin
if not estvide(g) then
init2 ( s1(g), h, flux ) ;
i := i + 1 ; x := adr ( g.cle, h ) ;
s := g.attr ; while not ( estvide ( s ) ) loop
ns := adr ( s.cle, h ) ;
ins ( -s.attr, x, h(ns).attr.prec_trie ) ;
ins ( s.attr, ns, h(x).attr.suiv_trie ) ;
i_i := ( g.cle, s.cle ) ;
ins ( i_i, zero, flux ) ;
s := s.alt ;
end loop ;
init2 ( s2(g), h, flux ) ;
end if ;
end init2 ;
-----------------------------------------------------------
procedure init_cap
( h : in out noeud_cell2s.table ;
f : out cap_ids.table ) is
s_s, s_p : item_arc ;
s : arc2s.table ;
begin
for x in 1..n loop
s_s := zero ;
s := h(x).attr.suiv_trie ;
while not ( estvide ( s ) ) loop
s_s := s_s + s.cle ;
s := s.alt ;
end loop ;
if estvide ( h(x).attr.suiv_trie ) then
s_s := infini ; end if ;
s_p := zero ;
s := h(x).attr.prec_trie ;
while not ( estvide ( s ) ) loop
s_p := s_p + abs ( s.cle ) ;
s := s.alt ;
end loop ;
if estvide ( h(x).attr.prec_trie ) then
h(x).attr.recu := s_s ;
h(x).attr.cap_noeud := s_s ;
ins ( s_s, x, f ) ;
else
h(x).attr.cap_noeud := min ( s_s, s_p ) ;
end if ;
end loop ;
end init_cap ;
begin
i := 0 ; init1 ( g, h ) ;
i := 0 ; init2 ( g, h, flux ) ;
init_cap ( h, f ) ;
end init ;
--------------------------------------------------------------
cx, m, anc : item_arc ;
x, ns : entier ;
x_ns, ns_x : id_id_liste ; s : arc2s.table ;
cont : string :="o" ;
i_i : id_id ;
begin
init ( flux ) ; init ( f ) ;
init ( g, h, f, flux ) ;
loop
exit when estvide ( f ) ;
del_min ( cx, x, f ) ; --h_x := adr ( x, h ) ;
s := h(x).attr.suiv_trie ;
while not ( estvide ( s ) ) loop
ns := s.attr ;
i_i := (h(x).cle,h(ns).cle) ;
x_ns := adr( i_i, flux) ;
m := min ( s.cle - x_ns.attr,
min ( h(ns).attr.cap_noeud -
h(ns).attr.flux_noeud,
h(x).attr.recu )) ;
if m > zero then
x_ns.attr := x_ns.attr + m ;
anc := h(ns).attr.flux_noeud ;
h(ns).attr.flux_noeud :=
h(ns).attr.flux_noeud + m ;
del ( anc, ns, f ) ;
ins ( h(ns).attr.flux_noeud, ns, f ) ;
h(ns).attr.recu := h(ns).attr.recu + m ;
h(x).attr.recu := h(x).attr.recu - m ;
exit when h(x).attr.recu = zero ;
end if ;
exit when h(x).attr.recu = zero ;
s := s.alt ;
end loop ;

if h(x).attr.recu > zero


and not estvide ( h(x).attr.suiv_trie ) then
s := h(x).attr.prec_trie ;
while not estvide ( s ) loop
ns := s.attr ;
ns_x := adr((h(ns).cle,h(x).cle),flux) ;
m := min ( ns_x.attr, h(x).attr.recu ) ;
if m > zero then
ns_x.attr := ns_x.attr - m ;
anc := h(ns).attr.recu ;
h(ns).attr.recu := h(ns).attr.recu + m ;
del ( anc, ns, f ) ;
ins ( h(ns).attr.recu, ns, f ) ;
h(x).attr.recu := h(x).attr.recu - m ;
end if ;
exit when h(x).attr.recu = zero ;
s := s.alt ;
end loop ;
h(x).attr.cap_noeud := h(x).attr.flux_noeud ;
end if ;

--text_io.put ( "H : " ) ; put ( h ) ; text_io.new_line ;


--text_io.put ( " ------------ " ) ; text_io.new_line ;
--text_io.put ( "F : " ) ; put ( f ) ; text_io.new_line ;
--text_io.put ( " ------------ " ) ; text_io.new_line ;
--text_io.put ( "FLUX : " ) ; put ( flux ) ;
--text_io.new_line ; text_io.put ( " ------------ " );
--text_io.new_line ;
end loop ;
--text_io.new_line ; text_io.put ( "Termin� ! " ) ;
return flux ;
end flot_max ;

----------------------------------------------------------------------------
-----------------------------------------------------------------
function coloriage ( g : graphe ) return coul_tab is
n : entier := card ( g ) ;
--package temp_ent8s is new temp_avls ( entier ) ; use temp_ent8s ;
package indexs is new temp_listes ( entier ) ; use indexs ;
type quat_bool is array ( ent8 range 1..4 ) of boolean ;
type quat_b is record
couleur : ent8 ;
nblibre : ent8 ;
libre : quat_bool ;
suiv : indexs.liste ; end record ;
package quat_b_tab is new
temp_tab_tries ( n, item_id, quat_b ) ; use quat_b_tab ;
h : quat_b_tab.table ;
package temp_nb is new temp_avls ( entier ) ; use temp_nb ;
type quat_temp_nb_table is
array ( ent8 range 1..4 ) of temp_nb.table ;
f : quat_temp_nb_table ;

i : entier ;

--------------------------------------------------------
procedure init1 ( g : graphe ; h : in out quat_b_tab.table ) is
--s : arcs.table ;
begin
if not estvide ( g ) then
init1 ( s1 ( g ), h ) ;
i := i + 1 ;
h(i).cle := g.cle ;
init ( h(i).attr.suiv ) ;
h(i).attr.couleur := 0 ;
init1 ( s2 ( g ), h ) ;
end if ;
end ;
------------------------------------------------------------------
procedure init2 ( g : graphe ; h : in out quat_b_tab.table ; f : in out
quat_temp_nb_table ) is
x, ns : entier ;
s : arcs.table ;
begin
if not estvide(g) then
init2 ( s1(g), h, f ) ;
i := i + 1 ; x := adr ( g.cle, h ) ;
s := g.attr ;
loop exit when estvide ( s ) ;
ns := adr ( s.cle, h ) ;
ins1 ( ns, h(x).attr.suiv ) ;
s := s.alt ;
end loop ;
ins ( x, f(4) ) ;
init2 ( s2(g), h, f ) ;
end if ;
end ;
-----------------------------------------------------------
y : coul_tab ;
x : entier ;
s : indexs.liste ;
cx, nbl : ent8 ;
ns : entier ;
fi : ent8 ;
-------------------------------------------------------
begin
init ( y ) ; --init ( h ) ;
for j in 1..4 loop
init ( f ( ent8(j) ) ) ;
end loop ;
i := 0 ; init1 ( g, h ) ;
i := 0 ; init2 ( g, h, f ) ;
for j in 1..n loop
for k in 1..4 loop
h(j).attr.libre(ent8(k)) := true ;
end loop ;
h(j).attr.nblibre := 4 ;
end loop ;
loop
fi := 1 ; while fi <= 4 loop
exit when not estvide ( f(fi) ) ;
fi := fi+1 ;
end loop ;
exit when fi > 4 ;
del_min ( x, f(fi) ) ;
cx := 1 ;
while cx <= 4 loop
exit when h(x).attr.libre(cx) ;
cx := cx + 1 ;
end loop ;
h(x).attr.couleur := cx ;
s := h(x).attr.suiv ;
while not estvide ( s ) loop
ns := s.info ;
if h(ns).attr.libre(cx) and h(ns).attr.couleur = 0 then
h(ns).attr.libre(cx) := false ;
nbl := h(ns).attr.nblibre ;
h(ns).attr.nblibre := nbl - 1 ;
del ( ns, f(nbl)) ;
ins ( ns, f(nbl-1)) ;
end if ;
s := s.alt ;
end loop ;
end loop ;
for j in 1..n loop
ins ( h(j).cle, h(j).attr.couleur, y ) ;
end loop ;
return y ;
end ;
--------------------------------------------------------------
--------------------------------------------------------------
end graphes ;

You might also like