<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=0019_-_BFS</id>
	<title>0019 - BFS - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=0019_-_BFS"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=0019_-_BFS&amp;action=history"/>
	<updated>2026-05-01T04:07:45Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://wiki.universitas.ro/index.php?title=0019_-_BFS&amp;diff=7967&amp;oldid=prev</id>
		<title>Simina: Pagină nouă: == Enunț == Se consideră un graf neorientat cu &lt;code&gt;n&lt;/code&gt; vârfuri și &lt;code&gt;m&lt;/code&gt; muchii și de asemenea un vârf &lt;code&gt;X&lt;/code&gt;.  = Cerinţa = Să se afișeze vârfurile vizitate în urma parcurgerii în lățime (Breadth First Search) a grafului, pornind din vârful &lt;code&gt;X&lt;/code&gt;.  = Date de intrare = Fişierul de intrare &lt;code&gt;BFSIN.txt&lt;/code&gt; conţine pe prima linie trei numere naturale &lt;code&gt;n m X&lt;/code&gt;, având următoarea semnificație: &lt;code&gt;n&lt;/code&gt; este n...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=0019_-_BFS&amp;diff=7967&amp;oldid=prev"/>
		<updated>2023-12-12T23:36:42Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă: == Enunț == Se consideră un graf neorientat cu &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; vârfuri și &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; muchii și de asemenea un vârf &amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt;.  = Cerinţa = Să se afișeze vârfurile vizitate în urma parcurgerii în lățime (Breadth First Search) a grafului, pornind din vârful &amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt;.  = Date de intrare = Fişierul de intrare &amp;lt;code&amp;gt;BFSIN.txt&amp;lt;/code&amp;gt; conţine pe prima linie trei numere naturale &amp;lt;code&amp;gt;n m X&amp;lt;/code&amp;gt;, având următoarea semnificație: &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; este n...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Enunț ==&lt;br /&gt;
Se consideră un graf neorientat cu &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; vârfuri și &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; muchii și de asemenea un vârf &amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Cerinţa =&lt;br /&gt;
Să se afișeze vârfurile vizitate în urma parcurgerii în lățime (Breadth First Search) a grafului, pornind din vârful &amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Date de intrare =&lt;br /&gt;
Fişierul de intrare &amp;lt;code&amp;gt;BFSIN.txt&amp;lt;/code&amp;gt; conţine pe prima linie trei numere naturale &amp;lt;code&amp;gt;n m X&amp;lt;/code&amp;gt;, având următoarea semnificație: &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; este numărul de noduri, &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; este numărul de muchii din graf, iar &amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; este vârful din care se începe parcurgerea. Următoarele &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; linii conțin câte două numere &amp;lt;code&amp;gt;x y&amp;lt;/code&amp;gt; cu semnificația că există muchie între &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;y&amp;lt;/code&amp;gt; .&lt;br /&gt;
&lt;br /&gt;
= Date de ieşire =&lt;br /&gt;
Fişierul de ieşire &amp;lt;code&amp;gt;BFSOUT.txt&amp;lt;/code&amp;gt; va conţine pe prima linie vârfurile vizitate în urma parcurgerii în lățime a grafului, aceasta începând din vârful &amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Restricţii şi precizări =&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;2 ≤ n ≤ 100&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;1 ≤ m ≤ n*(n-1)/2&amp;lt;/code&amp;gt;&lt;br /&gt;
* Graful poate sa nu fie conex!!!&lt;br /&gt;
* dacă în timpul parcurgerii, la un moment dat vârful curent al grafului este &amp;lt;code&amp;gt;Y&amp;lt;/code&amp;gt;, vecinii nevizitați ai lui &amp;lt;code&amp;gt;Y&amp;lt;/code&amp;gt; se vor analiza în ordine crescătoare.&lt;br /&gt;
&lt;br /&gt;
= Exemplul 1 =&lt;br /&gt;
&amp;lt;code&amp;gt;BFSIN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 5 7 2&lt;br /&gt;
 1 2&lt;br /&gt;
 3 2&lt;br /&gt;
 1 5&lt;br /&gt;
 2 4&lt;br /&gt;
 3 1&lt;br /&gt;
 4 5&lt;br /&gt;
 5 3 &lt;br /&gt;
&amp;lt;code&amp;gt;BFSOUT.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 2 1 3 4 5 &lt;br /&gt;
&lt;br /&gt;
= Exemplul 2 =&lt;br /&gt;
&amp;lt;code&amp;gt;BFSIN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 1 1 1&lt;br /&gt;
&amp;lt;code&amp;gt;BFSOUT.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 Datele nu corespund restrictiilor impuse&lt;br /&gt;
&lt;br /&gt;
== Rezolvare ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python3&amp;quot; line=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
from collections import deque&lt;br /&gt;
&lt;br /&gt;
def bfs(graf, start):&lt;br /&gt;
    vizitate = set()&lt;br /&gt;
    rezultat = []&lt;br /&gt;
&lt;br /&gt;
    coada = deque([start])&lt;br /&gt;
    vizitate.add(start)&lt;br /&gt;
&lt;br /&gt;
    while coada:&lt;br /&gt;
        nod_curent = coada.popleft()&lt;br /&gt;
        rezultat.append(nod_curent)&lt;br /&gt;
&lt;br /&gt;
        for vecin in sorted(graf[nod_curent]):&lt;br /&gt;
            if vecin not in vizitate:&lt;br /&gt;
                coada.append(vecin)&lt;br /&gt;
                vizitate.add(vecin)&lt;br /&gt;
&lt;br /&gt;
    return rezultat&lt;br /&gt;
&lt;br /&gt;
def citeste_graf(input_file):&lt;br /&gt;
    with open(input_file, &amp;#039;r&amp;#039;) as f:&lt;br /&gt;
        n, m, start = map(int, f.readline().split())&lt;br /&gt;
&lt;br /&gt;
        # Verificăm restricțiile&lt;br /&gt;
        if not (2 &amp;lt;= n &amp;lt;= 100 and 1 &amp;lt;= m &amp;lt;= n * (n - 1) / 2):&lt;br /&gt;
            with open(&amp;#039;BFSOUT.txt&amp;#039;, &amp;#039;w&amp;#039;) as fout:&lt;br /&gt;
                fout.write(&amp;quot;Datele nu corespund restrictiilor impuse&amp;quot;)&lt;br /&gt;
            return None, None&lt;br /&gt;
&lt;br /&gt;
        graf = {i: set() for i in range(1, n + 1)}&lt;br /&gt;
&lt;br /&gt;
        for _ in range(m):&lt;br /&gt;
            x, y = map(int, f.readline().split())&lt;br /&gt;
            graf[x].add(y)&lt;br /&gt;
            graf[y].add(x)&lt;br /&gt;
&lt;br /&gt;
    return graf, start&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    input_file = &amp;#039;BFSIN.txt&amp;#039;&lt;br /&gt;
    output_file = &amp;#039;BFSOUT.txt&amp;#039;&lt;br /&gt;
&lt;br /&gt;
    graf, start = citeste_graf(input_file)&lt;br /&gt;
&lt;br /&gt;
    # Dacă restricțiile nu sunt respectate, nu continuăm cu parcurgerea și afișăm mesajul în fișierul de ieșire&lt;br /&gt;
    if graf is not None and start is not None:&lt;br /&gt;
        rezultat = bfs(graf, start)&lt;br /&gt;
&lt;br /&gt;
        with open(output_file, &amp;#039;w&amp;#039;) as f:&lt;br /&gt;
            f.write(&amp;#039; &amp;#039;.join(map(str, rezultat)))&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    main()&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Simina</name></author>
	</entry>
</feed>